<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Reading a file, line by line</title>
	<atom:link href="http://bashcurescancer.com/reading-a-file-line-by-line.html/feed" rel="self" type="application/rss+xml" />
	<link>http://bashcurescancer.com/reading-a-file-line-by-line.html</link>
	<description>Learn the UNIX/Linux command line</description>
	<lastBuildDate>Mon, 06 Jun 2011 01:35:32 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: rahul benegal</title>
		<link>http://bashcurescancer.com/reading-a-file-line-by-line.html/comment-page-1#comment-35229</link>
		<dc:creator>rahul benegal</dc:creator>
		<pubDate>Sun, 27 Dec 2009 06:52:55 +0000</pubDate>
		<guid isPermaLink="false">http://bashcurescancer.com/?p=155#comment-35229</guid>
		<description>Do note that &quot;while read&quot; rather unexpectedly trims spaces in the line read !!
You would have to do a:


     while IFS=&#039;&#039; read line to avoid that.

Another thing that once tripped me was if i incrementing a counter in the loop, it was lost outside. After much hunting, i discovered that `while read` starts a subshell.

If you are populating an array or hash or counter etc in a `while read` loop, the only option I&#039;ve figured out is:

for line in $( cat file.txt ) ...

thanks
rahul</description>
		<content:encoded><![CDATA[<p>Do note that &#8220;while read&#8221; rather unexpectedly trims spaces in the line read !!<br />
You would have to do a:</p>
<p>     while IFS=&#8221; read line to avoid that.</p>
<p>Another thing that once tripped me was if i incrementing a counter in the loop, it was lost outside. After much hunting, i discovered that `while read` starts a subshell.</p>
<p>If you are populating an array or hash or counter etc in a `while read` loop, the only option I&#8217;ve figured out is:</p>
<p>for line in $( cat file.txt ) &#8230;</p>
<p>thanks<br />
rahul</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Splitting Strings Natively with the Shell: Why</title>
		<link>http://bashcurescancer.com/reading-a-file-line-by-line.html/comment-page-1#comment-34352</link>
		<dc:creator>Splitting Strings Natively with the Shell: Why</dc:creator>
		<pubDate>Wed, 09 Dec 2009 23:18:48 +0000</pubDate>
		<guid isPermaLink="false">http://bashcurescancer.com/?p=155#comment-34352</guid>
		<description>[...] strings into tokens or &#8220;words&#8221;. I previously discussed how to do this with the IFS variable and promised a more in depth discussion. Today, I will make the case on WHY to use IFS to split [...]</description>
		<content:encoded><![CDATA[<p>[...] strings into tokens or &#8220;words&#8221;. I previously discussed how to do this with the IFS variable and promised a more in depth discussion. Today, I will make the case on WHY to use IFS to split [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cris F.A. Johnson</title>
		<link>http://bashcurescancer.com/reading-a-file-line-by-line.html/comment-page-1#comment-33951</link>
		<dc:creator>Cris F.A. Johnson</dc:creator>
		<pubDate>Mon, 30 Nov 2009 08:56:12 +0000</pubDate>
		<guid isPermaLink="false">http://bashcurescancer.com/?p=155#comment-33951</guid>
		<description>&quot;you need to enclose the RHS of the assignment in quotes&quot;

You only need to quote the right-hand side when there is a literal whitespace character. (This is true for any Bourne-type shell.)


&quot;the “print” command needs to be changed to “echo”.&quot;

The print command should be changed to printf; echo is deprecated.</description>
		<content:encoded><![CDATA[<p>&#8220;you need to enclose the RHS of the assignment in quotes&#8221;</p>
<p>You only need to quote the right-hand side when there is a literal whitespace character. (This is true for any Bourne-type shell.)</p>
<p>&#8220;the “print” command needs to be changed to “echo”.&#8221;</p>
<p>The print command should be changed to printf; echo is deprecated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brock Noland</title>
		<link>http://bashcurescancer.com/reading-a-file-line-by-line.html/comment-page-1#comment-33920</link>
		<dc:creator>Brock Noland</dc:creator>
		<pubDate>Sun, 29 Nov 2009 18:13:35 +0000</pubDate>
		<guid isPermaLink="false">http://bashcurescancer.com/?p=155#comment-33920</guid>
		<description>Actually, Jeff my test does not show you are correct.  At least with BASH 3.1.17 and KSH 1993-12-28 r.

&lt;pre&gt;
$ cat test.sh
#!/bin/bash
echo \$IFS = &quot;&#039;$IFS&#039;&quot;
oldIFS=$IFS
echo \$oldIFS = &quot;&#039;$oldIFS&#039;&quot;

$ ./test.sh
$IFS = &#039;
&#039;
$oldIFS = &#039;
&#039;
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Actually, Jeff my test does not show you are correct.  At least with BASH 3.1.17 and KSH 1993-12-28 r.</p>
<pre>
$ cat test.sh
#!/bin/bash
echo \$IFS = "'$IFS'"
oldIFS=$IFS
echo \$oldIFS = "'$oldIFS'"

$ ./test.sh
$IFS = '
'
$oldIFS = '
'
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brock Noland</title>
		<link>http://bashcurescancer.com/reading-a-file-line-by-line.html/comment-page-1#comment-33919</link>
		<dc:creator>Brock Noland</dc:creator>
		<pubDate>Sun, 29 Nov 2009 18:08:20 +0000</pubDate>
		<guid isPermaLink="false">http://bashcurescancer.com/?p=155#comment-33919</guid>
		<description>Seth,  As I am sure you are aware, this works in BASH as well! ;)


Alex,

First, point is excellent.  I will update the post.  However, using a subshell whether it be awk, cut, sed or something else is much, much slower.</description>
		<content:encoded><![CDATA[<p>Seth,  As I am sure you are aware, this works in BASH as well! <img src='http://bashcurescancer.com/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Alex,</p>
<p>First, point is excellent.  I will update the post.  However, using a subshell whether it be awk, cut, sed or something else is much, much slower.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://bashcurescancer.com/reading-a-file-line-by-line.html/comment-page-1#comment-33870</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Sat, 28 Nov 2009 13:52:44 +0000</pubDate>
		<guid isPermaLink="false">http://bashcurescancer.com/?p=155#comment-33870</guid>
		<description>Commands like this are pretty risky:
oldIFS=$IFS
IFS=$oldIFS
If IFS included the newline character (as it does by default), or the tab character, it won&#039;t do any more after this command. For copying variables which might contain whitespace other than spaces, you need to enclose the RHS of the assignment in quotes:
oldIFS=&quot;$IFS&quot;
IFS=&quot;$oldIFS&quot;
But yes, manipulating IFS can be a nifty little trick from time to time. You can do the splitting with cut rather than awk, like this:
domain = $(echo $line &#124; cut -d &#039;&#124;&#039; -f 1)
ip = $(echo $line &#124; cut -d &#039;&#124;&#039; -f 2)
But in this case manipulating IFS is probably still quicker and even easier.</description>
		<content:encoded><![CDATA[<p>Commands like this are pretty risky:<br />
oldIFS=$IFS<br />
IFS=$oldIFS<br />
If IFS included the newline character (as it does by default), or the tab character, it won&#8217;t do any more after this command. For copying variables which might contain whitespace other than spaces, you need to enclose the RHS of the assignment in quotes:<br />
oldIFS=&#8221;$IFS&#8221;<br />
IFS=&#8221;$oldIFS&#8221;<br />
But yes, manipulating IFS can be a nifty little trick from time to time. You can do the splitting with cut rather than awk, like this:<br />
domain = $(echo $line | cut -d &#8216;|&#8217; -f 1)<br />
ip = $(echo $line | cut -d &#8216;|&#8217; -f 2)<br />
But in this case manipulating IFS is probably still quicker and even easier.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Seth</title>
		<link>http://bashcurescancer.com/reading-a-file-line-by-line.html/comment-page-1#comment-33755</link>
		<dc:creator>Seth</dc:creator>
		<pubDate>Wed, 25 Nov 2009 06:05:31 +0000</pubDate>
		<guid isPermaLink="false">http://bashcurescancer.com/?p=155#comment-33755</guid>
		<description>Good tip, but I thought this was _BASH_ cures cancer.  :)

For the above to work as a bash script the &quot;print&quot; command needs to be changed to &quot;echo&quot;.

Also, it would be nice if read would accept a word delimiter as a parameter the way AWK does.</description>
		<content:encoded><![CDATA[<p>Good tip, but I thought this was _BASH_ cures cancer.  <img src='http://bashcurescancer.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>For the above to work as a bash script the &#8220;print&#8221; command needs to be changed to &#8220;echo&#8221;.</p>
<p>Also, it would be nice if read would accept a word delimiter as a parameter the way AWK does.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.213 seconds -->
<!-- Cached page served by WP-Cache -->

