<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Personal blog of Chris Ergatides &#187; sed</title>
	<atom:link href="http://blog.ergatides.com/tag/sed/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ergatides.com</link>
	<description>jQuery, Linux, Technology, Web Design and random stuff.</description>
	<lastBuildDate>Mon, 06 Feb 2012 05:01:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>Using sed to search and replace contents of next line in a file.</title>
		<link>http://blog.ergatides.com/2012/01/24/using-sed-to-search-and-replace-contents-of-next-line-in-a-file/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-sed-to-search-and-replace-contents-of-next-line-in-a-file</link>
		<comments>http://blog.ergatides.com/2012/01/24/using-sed-to-search-and-replace-contents-of-next-line-in-a-file/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 13:50:19 +0000</pubDate>
		<dc:creator>Chris Ergatides</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[dyndns]]></category>
		<category><![CDATA[httpd]]></category>
		<category><![CDATA[no-ip]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://blog.ergatides.com/?p=1740</guid>


		<description><![CDATA[<p>This example will show you how easy it is using sed to find a particular line in a file and replace all or part of the next line. In this example, using htaccess we want to deny access to all clients except a particular dynamic IP (for your home connection for instance).</p>
<p>Your .htaccess file might look like this:</p>
<pre class="brush: plain; title: ; notranslate">
order deny,allow
deny from all

# allow from example.dyndns.org
allow from 8.8.4.4
</pre>
<p>And your sed code to update the allowed IP address might look like this:</p>
<pre class="brush: plain; title: ; notranslate">
sed -i -r &quot;/# allow from example.dyndns.org/I{\
n; s/([0-9]{1,3}.){3}[0-9]{1,3}/8.8.8.8/\
}&quot; .htaccess
</pre>
<p>Let&#8217;s look at it piece by piece.</p>
<ul>
<li><strong>/# allow from example.dyndns.org/I</strong> will search the file for this string. The I flag at the end makes the search case insensitive.</li>
<li><strong>n;</strong> will tell sed to continue processing on the next line.</li>
<li><strong>s/([0-9]{1,3}\.){3}[0-9]{1,3}/8.8.8.8/</strong> will regex search for an IP and replace the match with your updated IP (in this case 8.8.8.8).</li>
<li><strong>.htaccess</strong> is of course the input file to process.</li>
</ul>
<p>Note that in .htaccess files, comments must be on a line of their own. End of line or inline comments are not permitted. In cases where you do not need to replace a part of the next line but rather all of it, our replacement becomes <strong>s/.*/8.8.8.8/</strong></p>
<p>Related posts:</p><ol>
<li><a href='http://blog.ergatides.com/2011/09/07/securing-wordpress-dashboard-using-htaccess-behind-cloudflare-or-any-other-cdn/' rel='bookmark' title='Securing WordPress Dashboard using .htaccess behind CloudFlare (or any other CDN)'>Securing WordPress Dashboard using .htaccess behind CloudFlare (or any other CDN)</a></li>
<li><a href='http://blog.ergatides.com/2011/02/21/whats-my-ip-check-your-ip-address-from-the-command-line/' rel='bookmark' title='What&#8217;s my IP? Check your IP address from the command line.'>What&#8217;s my IP? Check your IP address from the command line.</a></li>
<li><a href='http://blog.ergatides.com/2010/02/24/handy-command-line-currency-converter/' rel='bookmark' title='Handy command line currency converter.'>Handy command line currency converter.</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>This example will show you how easy it is using sed to find a particular line in a file and replace all or part of the next line. In this example, using htaccess we want to deny access to all clients except a particular dynamic IP (for your home connection for instance).</p>
<p>Your .htaccess file might look like this:</p>
<pre class="brush: plain; title: ; notranslate">
order deny,allow
deny from all

# allow from example.dyndns.org
allow from 8.8.4.4
</pre>
<p>And your sed code to update the allowed IP address might look like this:</p>
<pre class="brush: plain; title: ; notranslate">
sed -i -r &quot;/# allow from example.dyndns.org/I{\
n; s/([0-9]{1,3}.){3}[0-9]{1,3}/8.8.8.8/\
}&quot; .htaccess
</pre>
<p>Let&#8217;s look at it piece by piece.</p>
<ul>
<li><strong>/# allow from example.dyndns.org/I</strong> will search the file for this string. The I flag at the end makes the search case insensitive.</li>
<li><strong>n;</strong> will tell sed to continue processing on the next line.</li>
<li><strong>s/([0-9]{1,3}\.){3}[0-9]{1,3}/8.8.8.8/</strong> will regex search for an IP and replace the match with your updated IP (in this case 8.8.8.8).</li>
<li><strong>.htaccess</strong> is of course the input file to process.</li>
</ul>
<p>Note that in .htaccess files, comments must be on a line of their own. End of line or inline comments are not permitted. In cases where you do not need to replace a part of the next line but rather all of it, our replacement becomes <strong>s/.*/8.8.8.8/</strong></p>
<p>Related posts:</p><ol>
<li><a href='http://blog.ergatides.com/2011/09/07/securing-wordpress-dashboard-using-htaccess-behind-cloudflare-or-any-other-cdn/' rel='bookmark' title='Securing WordPress Dashboard using .htaccess behind CloudFlare (or any other CDN)'>Securing WordPress Dashboard using .htaccess behind CloudFlare (or any other CDN)</a></li>
<li><a href='http://blog.ergatides.com/2011/02/21/whats-my-ip-check-your-ip-address-from-the-command-line/' rel='bookmark' title='What&#8217;s my IP? Check your IP address from the command line.'>What&#8217;s my IP? Check your IP address from the command line.</a></li>
<li><a href='http://blog.ergatides.com/2010/02/24/handy-command-line-currency-converter/' rel='bookmark' title='Handy command line currency converter.'>Handy command line currency converter.</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://blog.ergatides.com/2012/01/24/using-sed-to-search-and-replace-contents-of-next-line-in-a-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Handy command line currency converter.</title>
		<link>http://blog.ergatides.com/2010/02/24/handy-command-line-currency-converter/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=handy-command-line-currency-converter</link>
		<comments>http://blog.ergatides.com/2010/02/24/handy-command-line-currency-converter/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 21:15:18 +0000</pubDate>
		<dc:creator>Chris Ergatides</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[sed]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://blog.ergatides.com/?p=154</guid>


		<description><![CDATA[<p>In 2003, I wrote a custom Amazon-like shopping cart script. Back then, my web scraping skills weren&#8217;t, ahem, the best they could have been. For the site&#8217;s currency conversions, I had a cron job pull down 3 sets of converstions, calculated the average for each currency pair, then stored the result locally for my use.</p>
<p>At the time, I did this hoping that in a worse-case-scenario, a maximum of 2 sources would die in any weekend. Luckily, the sources lasted for the 3 years whilst I was working for that company (and apparently for another two after that). Now, I hear, they are manually updating a static text file&#8230; once in a while.</p>
<p>Today, I have no such needs but as always I am constantly looking for new ways to do things, should the need arise. I also needed a simple currency converter for doing small calculations like how much to top up a prepaid credit card by to purchase something from eBay which is listed in another currency.</p>
<p>Full credits to <a href="http://commandliners.com/2009/02/currency-conversion-script/">rafacas at commandliners</a> for the inspiration and as he points out, note that the script uses <a href="http://www.google.com/finance">Google Finance’s page</a>.</p>
<pre class="brush: bash; title: ; notranslate">
#!/bin/bash

# 2009 - Rafa Casado - http://bit.ly/bYZwex
# 2010 - Chris Ergatides - http://bit.ly/9XjAUz

if [ $# -lt 2 ]; then
    echo -e &quot;\nUsage: $0 currency1 currency2 amount&quot;
    echo &quot;Default: $0 GBP EUR 1&quot;
    echo &quot;Example 1: $0 USD GBP&quot;
    echo &quot;Example 2: $0 GBP USD 42&quot;
fi

toUpper() {
    echo $@ | tr &quot;[:lower:]&quot; &quot;[:upper:]&quot;
}

if [ -n &quot;$1&quot; ]; then FROM=$(toUpper &quot;$1&quot;); else FROM=GBP; fi
if [ -n &quot;$2&quot; ]; then TO=$(toUpper &quot;$2&quot;); else TO=EUR; fi
if [ $TO == $FROM ]; then echo 'Nothing to do!'; exit 2; fi
if [ -n &quot;$3&quot; ]; then A=$3; else A=1; fi

CONVERTER=&quot;http://www.google.com/finance/converter?a=$A&amp;from=$FROM&amp;to=$TO&quot;

RESULT=`wget -nv -O - &quot;$CONVERTER&quot; 2&gt;&amp;1 | \
sed -n -e 's/.*&lt;span class=bld&gt;\(.*\)&lt;\/span&gt;.*/\1/p'`

echo -e &quot;\nResult: $A $FROM = $RESULT\n&quot;
</pre>
<p>So, let&#8217;s say you&#8217;ve named the script currency.sh and made it executable with chmod +x, what does the output look like?</p>
<pre class="brush: bash; title: ; notranslate">
#Called without any parameters:
[chris@r500 ~/bin]$ ./currency.sh

Usage: /home/chris/bin/currency.sh currency1 currency2 amount
Default: /home/chris/bin/currency.sh GBP EUR 1
Example 1: /home/chris/bin/currency.sh USD GBP
Example 2: /home/chris/bin/currency.sh GBP USD 42

Result: 1 GBP = 1.1387 EUR
</pre>
<pre class="brush: bash; title: ; notranslate">
#Called with example 1's parameters:
[chris@r500 ~/bin]$ ./currency.sh usd gbp

Result: 1 USD = 0.6475 GBP
</pre>
<pre class="brush: bash; title: ; notranslate">
#Called with example 2's parameters
[chris@r500 ~/bin]$ ./currency.sh gbp usd 42

Result: 42 GBP = 64.8606 USD
</pre>
<p>Related posts:</p><ol>
<li><a href='http://blog.ergatides.com/2011/02/21/whats-my-ip-check-your-ip-address-from-the-command-line/' rel='bookmark' title='What&#8217;s my IP? Check your IP address from the command line.'>What&#8217;s my IP? Check your IP address from the command line.</a></li>
<li><a href='http://blog.ergatides.com/2012/01/13/ternary-operator-for-bash-scripting/' rel='bookmark' title='Ternary operator for bash scripting.'>Ternary operator for bash scripting.</a></li>
<li><a href='http://blog.ergatides.com/2012/01/24/using-sed-to-search-and-replace-contents-of-next-line-in-a-file/' rel='bookmark' title='Using sed to search and replace contents of next line in a file.'>Using sed to search and replace contents of next line in a file.</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>In 2003, I wrote a custom Amazon-like shopping cart script. Back then, my web scraping skills weren&#8217;t, ahem, the best they could have been. For the site&#8217;s currency conversions, I had a cron job pull down 3 sets of converstions, calculated the average for each currency pair, then stored the result locally for my use.</p>
<p>At the time, I did this hoping that in a worse-case-scenario, a maximum of 2 sources would die in any weekend. Luckily, the sources lasted for the 3 years whilst I was working for that company (and apparently for another two after that). Now, I hear, they are manually updating a static text file&#8230; once in a while.</p>
<p>Today, I have no such needs but as always I am constantly looking for new ways to do things, should the need arise. I also needed a simple currency converter for doing small calculations like how much to top up a prepaid credit card by to purchase something from eBay which is listed in another currency.</p>
<p>Full credits to <a href="http://commandliners.com/2009/02/currency-conversion-script/">rafacas at commandliners</a> for the inspiration and as he points out, note that the script uses <a href="http://www.google.com/finance">Google Finance’s page</a>.</p>
<pre class="brush: bash; title: ; notranslate">
#!/bin/bash

# 2009 - Rafa Casado - http://bit.ly/bYZwex
# 2010 - Chris Ergatides - http://bit.ly/9XjAUz

if [ $# -lt 2 ]; then
    echo -e &quot;\nUsage: $0 currency1 currency2 amount&quot;
    echo &quot;Default: $0 GBP EUR 1&quot;
    echo &quot;Example 1: $0 USD GBP&quot;
    echo &quot;Example 2: $0 GBP USD 42&quot;
fi

toUpper() {
    echo $@ | tr &quot;[:lower:]&quot; &quot;[:upper:]&quot;
}

if [ -n &quot;$1&quot; ]; then FROM=$(toUpper &quot;$1&quot;); else FROM=GBP; fi
if [ -n &quot;$2&quot; ]; then TO=$(toUpper &quot;$2&quot;); else TO=EUR; fi
if [ $TO == $FROM ]; then echo 'Nothing to do!'; exit 2; fi
if [ -n &quot;$3&quot; ]; then A=$3; else A=1; fi

CONVERTER=&quot;http://www.google.com/finance/converter?a=$A&amp;from=$FROM&amp;to=$TO&quot;

RESULT=`wget -nv -O - &quot;$CONVERTER&quot; 2&gt;&amp;1 | \
sed -n -e 's/.*&lt;span class=bld&gt;\(.*\)&lt;\/span&gt;.*/\1/p'`

echo -e &quot;\nResult: $A $FROM = $RESULT\n&quot;
</pre>
<p>So, let&#8217;s say you&#8217;ve named the script currency.sh and made it executable with chmod +x, what does the output look like?</p>
<pre class="brush: bash; title: ; notranslate">
#Called without any parameters:
[chris@r500 ~/bin]$ ./currency.sh

Usage: /home/chris/bin/currency.sh currency1 currency2 amount
Default: /home/chris/bin/currency.sh GBP EUR 1
Example 1: /home/chris/bin/currency.sh USD GBP
Example 2: /home/chris/bin/currency.sh GBP USD 42

Result: 1 GBP = 1.1387 EUR
</pre>
<pre class="brush: bash; title: ; notranslate">
#Called with example 1's parameters:
[chris@r500 ~/bin]$ ./currency.sh usd gbp

Result: 1 USD = 0.6475 GBP
</pre>
<pre class="brush: bash; title: ; notranslate">
#Called with example 2's parameters
[chris@r500 ~/bin]$ ./currency.sh gbp usd 42

Result: 42 GBP = 64.8606 USD
</pre>
<p>Related posts:</p><ol>
<li><a href='http://blog.ergatides.com/2011/02/21/whats-my-ip-check-your-ip-address-from-the-command-line/' rel='bookmark' title='What&#8217;s my IP? Check your IP address from the command line.'>What&#8217;s my IP? Check your IP address from the command line.</a></li>
<li><a href='http://blog.ergatides.com/2012/01/13/ternary-operator-for-bash-scripting/' rel='bookmark' title='Ternary operator for bash scripting.'>Ternary operator for bash scripting.</a></li>
<li><a href='http://blog.ergatides.com/2012/01/24/using-sed-to-search-and-replace-contents-of-next-line-in-a-file/' rel='bookmark' title='Using sed to search and replace contents of next line in a file.'>Using sed to search and replace contents of next line in a file.</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://blog.ergatides.com/2010/02/24/handy-command-line-currency-converter/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

