<?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: 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>Making Mistakes as Fast as I Can.</description>
	<lastBuildDate>Tue, 16 Mar 2010 20:53:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Andre</title>
		<link>http://antonolsen.com/2005/03/23/read-a-file-with-bash/comment-page-2/#comment-1561</link>
		<dc:creator>Andre</dc:creator>
		<pubDate>Thu, 30 Apr 2009 18:21:39 +0000</pubDate>
		<guid isPermaLink="false">http://antonolsen.com/?p=24#comment-1561</guid>
		<description>How to read using bash, just one single fuk**ng file!?!?</description>
		<content:encoded><![CDATA[<p>How to read using bash, just one single fuk**ng file!?!?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stefano</title>
		<link>http://antonolsen.com/2005/03/23/read-a-file-with-bash/comment-page-2/#comment-1251</link>
		<dc:creator>stefano</dc:creator>
		<pubDate>Fri, 05 Dec 2008 18:16:56 +0000</pubDate>
		<guid isPermaLink="false">http://antonolsen.com/?p=24#comment-1251</guid>
		<description>Thanks a lot :-)</description>
		<content:encoded><![CDATA[<p>Thanks 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: Anton</title>
		<link>http://antonolsen.com/2005/03/23/read-a-file-with-bash/comment-page-2/#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&#039;t doing anything for you.  Put &#039;fred=&quot;&quot;&#039; at the top of the loop, and just append to it in each &quot;if&quot;.

You can use variable variables inside the loop so you don&#039;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=&quot;&quot;
for VAR in col1 col2 col3 col4; do
   if [ -n &quot;${!VAR} ] ; then
      fred=$fred&quot;${!VAR}&quot;
   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-page-2/#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=&#039;&#124;&#039;;

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

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

I want to know - can I substitute &#039;col1 - col4&#039; for a variable, since there is about 40 fields per line on the &quot;real&quot; 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 &#8211; can I substitute &#8216;col1 &#8211; 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-page-2/#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' /> </p>
<p>I have this BASH,sed, awk, blog to share with all of you.</p>
<p><a href="http://unstableme.blogspot.com/" rel="nofollow">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-page-2/#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-page-2/#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-page-2/#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&#039;s response to Geir:

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

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

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

$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&#039;s a zero)  flag. find -print0 and xargs -0 should never fail because UNIX paths won&#039;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-page-2/#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-page-2/#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>
</channel>
</rss>
