<?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"
	>
<channel>
	<title>Comments on: read a file with bash</title>
	<atom:link href="http://antonolsen.com/2005/03/23/read-a-file-with-bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://antonolsen.com/2005/03/23/read-a-file-with-bash/</link>
	<description>Just another LR2 Blogs weblog</description>
	<pubDate>Fri, 21 Nov 2008 21:47:53 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
		<item>
		<title>By: Anton</title>
		<link>http://antonolsen.com/2005/03/23/read-a-file-with-bash/#comment-512</link>
		<dc:creator>Anton</dc:creator>
		<pubDate>Tue, 02 Sep 2008 14:31:04 +0000</pubDate>
		<guid isPermaLink="false">http://antonolsen.com/?p=24#comment-512</guid>
		<description>In all cases above you can remove the else clause, it isn't doing anything for you.  Put 'fred=""' at the top of the loop, and just append to it in each "if".

You can use variable variables inside the loop so you don't have to repeat each if block.

To see how they work, try this:
VAR1=abc
abc=123
echo ${!VAR1}

Inside your while read loop you could do something like this:

fred=""
for VAR in col1 col2 col3 col4; do
   if [ -n "${!VAR} ] ; then
      fred=$fred"${!VAR}"
   fi
done</description>
		<content:encoded><![CDATA[<p>In all cases above you can remove the else clause, it isn&#8217;t doing anything for you.  Put &#8216;fred=&#8221;"&#8216; at the top of the loop, and just append to it in each &#8220;if&#8221;.</p>
<p>You can use variable variables inside the loop so you don&#8217;t have to repeat each if block.</p>
<p>To see how they work, try this:<br />
VAR1=abc<br />
abc=123<br />
echo ${!VAR1}</p>
<p>Inside your while read loop you could do something like this:</p>
<p>fred=&#8221;"<br />
for VAR in col1 col2 col3 col4; do<br />
   if [ -n "${!VAR} ] ; then<br />
      fred=$fred&#8221;${!VAR}&#8221;<br />
   fi<br />
done</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Francois</title>
		<link>http://antonolsen.com/2005/03/23/read-a-file-with-bash/#comment-513</link>
		<dc:creator>Francois</dc:creator>
		<pubDate>Tue, 02 Sep 2008 10:20:09 +0000</pubDate>
		<guid isPermaLink="false">http://antonolsen.com/?p=24#comment-513</guid>
		<description>Anton,

I have the following code to read in a file an process accordingly :

#/tmp/test.txt looks like this :
# 0-1&#124;0-2&#124;0-3&#124;0-4
# 1-1&#124;&#124;1-3&#124;1-4
# &#124;2-2&#124;2-3&#124;2-4
# 3-1&#124;3-2&#124;&#124;3-4

FILENAME=$1; #input filename
OIFS=$IFS;
IFS='&#124;';

while read col1 col2 col3 col4
  do
    if [ -n "$col1" ]; then
      fred="col1 = '$col1', ";
    else
      fred="";
    fi
    if [ -n "$col2" ]; then
      fred=$fred"col2 = '$col2', ";
    else
      fred=$fred;
    fi
    if [ -n "$col3" ]; then
      fred=$fred"col3 = '$col3', ";
    else
      fred=$fred;
    fi
    if [ -n "$col4" ]; then
      fred=$fred"col4 = '$col4'";
    else
      fred=$fred;
    fi
    echo $fred;
done &#60;$FILENAME

#the output looks like this
col1 = '0-1', col2 = '0-2', col3 = '0-3', col4 = '0-4'
col1 = '1-1', col3 = '1-3', col4 = '1-4'
col2 = '2-2', col3 = '2-3', col4 = '2-4'
col1 = '3-1', col2 = '3-2', col4 = '3-4'

I want to know - can I substitute 'col1 - col4' for a variable, since there is about 40 fields per line on the "real" file and to do more or less the same validation repetitively seems pointless.

any suggestions would be appreciated.

thanks</description>
		<content:encoded><![CDATA[<p>Anton,</p>
<p>I have the following code to read in a file an process accordingly :</p>
<p>#/tmp/test.txt looks like this :<br />
# 0-1|0-2|0-3|0-4<br />
# 1-1||1-3|1-4<br />
# |2-2|2-3|2-4<br />
# 3-1|3-2||3-4</p>
<p>FILENAME=$1; #input filename<br />
OIFS=$IFS;<br />
IFS=&#8217;|';</p>
<p>while read col1 col2 col3 col4<br />
  do<br />
    if [ -n "$col1" ]; then<br />
      fred=&#8221;col1 = &#8216;$col1&#8242;, &#8220;;<br />
    else<br />
      fred=&#8221;";<br />
    fi<br />
    if [ -n "$col2" ]; then<br />
      fred=$fred&#8221;col2 = &#8216;$col2&#8242;, &#8220;;<br />
    else<br />
      fred=$fred;<br />
    fi<br />
    if [ -n "$col3" ]; then<br />
      fred=$fred&#8221;col3 = &#8216;$col3&#8242;, &#8220;;<br />
    else<br />
      fred=$fred;<br />
    fi<br />
    if [ -n "$col4" ]; then<br />
      fred=$fred&#8221;col4 = &#8216;$col4&#8242;&#8221;;<br />
    else<br />
      fred=$fred;<br />
    fi<br />
    echo $fred;<br />
done &lt;$FILENAME</p>
<p>#the output looks like this<br />
col1 = &#8216;0-1&#8242;, col2 = &#8216;0-2&#8242;, col3 = &#8216;0-3&#8242;, col4 = &#8216;0-4&#8242;<br />
col1 = &#8216;1-1&#8242;, col3 = &#8216;1-3&#8242;, col4 = &#8216;1-4&#8242;<br />
col2 = &#8216;2-2&#8242;, col3 = &#8216;2-3&#8242;, col4 = &#8216;2-4&#8242;<br />
col1 = &#8216;3-1&#8242;, col2 = &#8216;3-2&#8242;, col4 = &#8216;3-4&#8242;</p>
<p>I want to know - can I substitute &#8216;col1 - col4&#8242; for a variable, since there is about 40 fields per line on the &#8220;real&#8221; file and to do more or less the same validation repetitively seems pointless.</p>
<p>any suggestions would be appreciated.</p>
<p>thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jadu Kumar Saikia</title>
		<link>http://antonolsen.com/2005/03/23/read-a-file-with-bash/#comment-510</link>
		<dc:creator>Jadu Kumar Saikia</dc:creator>
		<pubDate>Fri, 29 Feb 2008 18:28:49 +0000</pubDate>
		<guid isPermaLink="false">http://antonolsen.com/?p=24#comment-510</guid>
		<description>Nice page :-)

I have this BASH,sed, awk, blog to share with all of you.

http://unstableme.blogspot.com/</description>
		<content:encoded><![CDATA[<p>Nice page <img src='http://antonolsen.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
I have this BASH,sed, awk, blog to share with all of you.</p>
<p><a href="http://unstableme.blogspot.com/" rel="nofollow" onclick="javascript:urchinTracker ('/outbound/comment/unstableme.blogspot.com');">http://unstableme.blogspot.com/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stevan Ljuljdurovic</title>
		<link>http://antonolsen.com/2005/03/23/read-a-file-with-bash/#comment-511</link>
		<dc:creator>Stevan Ljuljdurovic</dc:creator>
		<pubDate>Sat, 16 Feb 2008 01:17:03 +0000</pubDate>
		<guid isPermaLink="false">http://antonolsen.com/?p=24#comment-511</guid>
		<description>Helped me fix my problem, thanks.</description>
		<content:encoded><![CDATA[<p>Helped me fix my problem, thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anton</title>
		<link>http://antonolsen.com/2005/03/23/read-a-file-with-bash/#comment-509</link>
		<dc:creator>Anton</dc:creator>
		<pubDate>Fri, 05 Oct 2007 22:33:17 +0000</pubDate>
		<guid isPermaLink="false">http://antonolsen.com/?p=24#comment-509</guid>
		<description>@LightningCrash

Thanks for the tips on quoting and nulls!</description>
		<content:encoded><![CDATA[<p>@LightningCrash</p>
<p>Thanks for the tips on quoting and nulls!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: LightningCrash</title>
		<link>http://antonolsen.com/2005/03/23/read-a-file-with-bash/#comment-508</link>
		<dc:creator>LightningCrash</dc:creator>
		<pubDate>Fri, 05 Oct 2007 14:17:00 +0000</pubDate>
		<guid isPermaLink="false">http://antonolsen.com/?p=24#comment-508</guid>
		<description>@Anton's response to Geir:

You can work around the spaces problem very easily in xargs, don't write it off so soon.

$ cat files.lst
File with spaces.txt
file2.txt

$ cat files.lst &#124;xargs -i rm -v "{}"
removed `File with spaces.txt'
removed `file2.txt'

$cat files.lst &#124;xargs -i echo {}
File with spaces.txt
file2.txt

Using the find command from findutils, you can also specify -print0 to print NUL characters for spaces instead of whitespace. Xargs can them interpret the whitespace via the -0 (that's a zero)  flag. find -print0 and xargs -0 should never fail because UNIX paths won't contain NULs.</description>
		<content:encoded><![CDATA[<p>@Anton&#8217;s response to Geir:</p>
<p>You can work around the spaces problem very easily in xargs, don&#8217;t write it off so soon.</p>
<p>$ cat files.lst<br />
File with spaces.txt<br />
file2.txt</p>
<p>$ cat files.lst |xargs -i rm -v &#8220;{}&#8221;<br />
removed `File with spaces.txt&#8217;<br />
removed `file2.txt&#8217;</p>
<p>$cat files.lst |xargs -i echo {}<br />
File with spaces.txt<br />
file2.txt</p>
<p>Using the find command from findutils, you can also specify -print0 to print NUL characters for spaces instead of whitespace. Xargs can them interpret the whitespace via the -0 (that&#8217;s a zero)  flag. find -print0 and xargs -0 should never fail because UNIX paths won&#8217;t contain NULs.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tiko</title>
		<link>http://antonolsen.com/2005/03/23/read-a-file-with-bash/#comment-507</link>
		<dc:creator>tiko</dc:creator>
		<pubDate>Wed, 26 Sep 2007 22:26:11 +0000</pubDate>
		<guid isPermaLink="false">http://antonolsen.com/?p=24#comment-507</guid>
		<description>Thank you!  That helped a lot.  :)</description>
		<content:encoded><![CDATA[<p>Thank you!  That helped a lot.  <img src='http://antonolsen.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gibbo_monster</title>
		<link>http://antonolsen.com/2005/03/23/read-a-file-with-bash/#comment-506</link>
		<dc:creator>gibbo_monster</dc:creator>
		<pubDate>Fri, 31 Aug 2007 09:35:47 +0000</pubDate>
		<guid isPermaLink="false">http://antonolsen.com/?p=24#comment-506</guid>
		<description>Big big  help, thanks a lot.

g_m.</description>
		<content:encoded><![CDATA[<p>Big big  help, thanks a lot.</p>
<p>g_m.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anton</title>
		<link>http://antonolsen.com/2005/03/23/read-a-file-with-bash/#comment-505</link>
		<dc:creator>Anton</dc:creator>
		<pubDate>Fri, 24 Aug 2007 16:46:09 +0000</pubDate>
		<guid isPermaLink="false">http://antonolsen.com/?p=24#comment-505</guid>
		<description>Ionic,

The point isn't to replace cat, or get find to output the files.  That is just a simple example.

The intent is to demonstrate reading something line by line and acting upon it.  echo can be replaced with any other command, or a series of commands and maybe some logic.

# find . -name '*jpg' &#124; while read pic ; do
convert ${pic} ${pic}.png
mv ${pic}.png /some/new/dir
done

Or you could pull pic apart and, replace the jpg with png properly, and maybe resize if it's over X bytes, etc.

I like to escape my shell vars so I don't get into problems with concatination.  It's a personal preference and while other methods work, this is what I like.  That way I don't accidently try something like "$PATHnewdir" which isn't the same as "${PATH}newdir"</description>
		<content:encoded><![CDATA[<p>Ionic,</p>
<p>The point isn&#8217;t to replace cat, or get find to output the files.  That is just a simple example.</p>
<p>The intent is to demonstrate reading something line by line and acting upon it.  echo can be replaced with any other command, or a series of commands and maybe some logic.</p>
<p># find . -name &#8216;*jpg&#8217; | while read pic ; do<br />
convert ${pic} ${pic}.png<br />
mv ${pic}.png /some/new/dir<br />
done</p>
<p>Or you could pull pic apart and, replace the jpg with png properly, and maybe resize if it&#8217;s over X bytes, etc.</p>
<p>I like to escape my shell vars so I don&#8217;t get into problems with concatination.  It&#8217;s a personal preference and while other methods work, this is what I like.  That way I don&#8217;t accidently try something like &#8220;$PATHnewdir&#8221; which isn&#8217;t the same as &#8220;${PATH}newdir&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jwimmer</title>
		<link>http://antonolsen.com/2005/03/23/read-a-file-with-bash/#comment-500</link>
		<dc:creator>jwimmer</dc:creator>
		<pubDate>Fri, 24 Aug 2007 12:58:36 +0000</pubDate>
		<guid isPermaLink="false">http://antonolsen.com/?p=24#comment-500</guid>
		<description>Anton,

Thank you for the response.  Your example worked just great.  Thank you very much!  Now I need to pass the file generated by redirecting into a file, i.e., the echo $x1 $x2 $x3 $x4 ; done &#62; ids2.txt and passing that as an argument into another bash script.  Thanks again</description>
		<content:encoded><![CDATA[<p>Anton,</p>
<p>Thank you for the response.  Your example worked just great.  Thank you very much!  Now I need to pass the file generated by redirecting into a file, i.e., the echo $x1 $x2 $x3 $x4 ; done &gt; ids2.txt and passing that as an argument into another bash script.  Thanks again</p>
]]></content:encoded>
	</item>
</channel>
</rss>
