<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Bash Script : Print lines of a file in reverse order</title>
	<atom:link href="http://phoxis.org/2010/03/04/bash-print-lines-reversed/feed/" rel="self" type="application/rss+xml" />
	<link>http://phoxis.org/2010/03/04/bash-print-lines-reversed/</link>
	<description>Don&#039;t just think ... Do it.</description>
	<lastBuildDate>Sun, 05 Feb 2012 17:26:22 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: phoxis</title>
		<link>http://phoxis.org/2010/03/04/bash-print-lines-reversed/#comment-2277</link>
		<dc:creator><![CDATA[phoxis]]></dc:creator>
		<pubDate>Sat, 13 Aug 2011 08:59:21 +0000</pubDate>
		<guid isPermaLink="false">http://phoxis.wordpress.com/?p=847#comment-2277</guid>
		<description><![CDATA[There are a lot of other ways, and definitely i would prefer other interpreted language or better write a C/C++ program. This was just a demonstration of achieving the thing using bash builtins.]]></description>
		<content:encoded><![CDATA[<p>There are a lot of other ways, and definitely i would prefer other interpreted language or better write a C/C++ program. This was just a demonstration of achieving the thing using bash builtins.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: outlyer</title>
		<link>http://phoxis.org/2010/03/04/bash-print-lines-reversed/#comment-2276</link>
		<dc:creator><![CDATA[outlyer]]></dc:creator>
		<pubDate>Sat, 13 Aug 2011 04:20:28 +0000</pubDate>
		<guid isPermaLink="false">http://phoxis.wordpress.com/?p=847#comment-2276</guid>
		<description><![CDATA[Probably any other (interpreted) language choice would be better (i.e. *much* faster for large files) than bash, but you probably know that already :P

I usually forget about tac since I need to do this about once a year, and end up re-inventing the wheel. Off the top of my head:

[sourcecode language=&quot;bash&quot; light=&quot;true&quot;]$ grep -n ^ FILE &#124; sort -nr -t: -k1 &#124; cut -d: -f2[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Probably any other (interpreted) language choice would be better (i.e. *much* faster for large files) than bash, but you probably know that already :P</p>
<p>I usually forget about tac since I need to do this about once a year, and end up re-inventing the wheel. Off the top of my head:</p>
<pre class="brush: bash; light: true;">$ grep -n ^ FILE | sort -nr -t: -k1 | cut -d: -f2</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: phoxis</title>
		<link>http://phoxis.org/2010/03/04/bash-print-lines-reversed/#comment-1812</link>
		<dc:creator><![CDATA[phoxis]]></dc:creator>
		<pubDate>Sat, 14 May 2011 13:57:25 +0000</pubDate>
		<guid isPermaLink="false">http://phoxis.wordpress.com/?p=847#comment-1812</guid>
		<description><![CDATA[Hello,
There is no &lt;tt&gt;-r&lt;/tt&gt; in tail as per the manual of tail. &lt;a href=&quot;http://unixhelp.ed.ac.uk/CGI/man-cgi?tail&quot; rel=&quot;nofollow&quot;&gt;http://unixhelp.ed.ac.uk/CGI/man-cgi?tail&lt;/a&gt; . Under what system are you working and the tail you are using is of which software package component. The standard coreutilities and the busybox tail does not have such option.]]></description>
		<content:encoded><![CDATA[<p>Hello,<br />
There is no <tt>-r</tt> in tail as per the manual of tail. <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?tail" rel="nofollow">http://unixhelp.ed.ac.uk/CGI/man-cgi?tail</a> . Under what system are you working and the tail you are using is of which software package component. The standard coreutilities and the busybox tail does not have such option.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xavier Smet</title>
		<link>http://phoxis.org/2010/03/04/bash-print-lines-reversed/#comment-1811</link>
		<dc:creator><![CDATA[Xavier Smet]]></dc:creator>
		<pubDate>Sat, 14 May 2011 12:05:38 +0000</pubDate>
		<guid isPermaLink="false">http://phoxis.wordpress.com/?p=847#comment-1811</guid>
		<description><![CDATA[I stumbled upon this, and I think it doesn&#039;t have to load the entire file into memory (as opposed to the sed solution, and most others): 
[sourcecode language=&quot;bash&quot;]
tail -r
[/sourcecode]
Example:
[sourcecode language=&quot;bash&quot;]
[~]&gt; cat testfile.txt 
first line
second line
line 3
last line
[~]&gt; tail -r testfile.txt 
last line
line 3
second line
first line
[/sourcecode]
FYI, from the man page: &quot;The default for the -r option is to display all of the input.&quot;]]></description>
		<content:encoded><![CDATA[<p>I stumbled upon this, and I think it doesn&#8217;t have to load the entire file into memory (as opposed to the sed solution, and most others): </p>
<pre class="brush: bash;">
tail -r
</pre>
<p>Example:</p>
<pre class="brush: bash;">
[~]&gt; cat testfile.txt
first line
second line
line 3
last line
[~]&gt; tail -r testfile.txt
last line
line 3
second line
first line
</pre>
<p>FYI, from the man page: &#8220;The default for the -r option is to display all of the input.&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: phoxis</title>
		<link>http://phoxis.org/2010/03/04/bash-print-lines-reversed/#comment-1804</link>
		<dc:creator><![CDATA[phoxis]]></dc:creator>
		<pubDate>Fri, 13 May 2011 16:56:50 +0000</pubDate>
		<guid isPermaLink="false">http://phoxis.wordpress.com/?p=847#comment-1804</guid>
		<description><![CDATA[&lt;ol&gt;
&lt;li&gt;&lt;em&gt;&quot;is it really necessary to set IFS=$’\n’&quot;&lt;/em&gt;
&lt;p&gt;
Yes it is necessary to set this. Because the default IFS characters seperate the fields of by the blankspace and the tab and the new line. So if you read one single line into the array like that with the default IFS value then you will end up with one single word in each array location. For example if file has a line : &lt;tt&gt;hello man this is s test&lt;/tt&gt; and you execute the &lt;tt&gt;arr=( $( &lt; $file_name ) )&lt;/tt&gt; , then &lt;tt&gt;arr[0]&lt;/tt&gt; will be &quot;hello&quot;, &lt;tt&gt;arr[1]&lt;/tt&gt; will be &quot;man&quot; etc. You can test the result by not setting the &lt;tt&gt;IFS=$&#039;\n&#039;&lt;/tt&gt;. Setting this makes the field seperator only the new lines. (Note that the code version you have posted in the comment contains the &lt;tt&gt;IFS=$&#039;\n&#039;&lt;/tt&gt; line.
&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;&lt;em&gt;&quot;Following on from your assumption that the ‘while read -r line’ test evaluated false if there was no newline,&quot;&lt;/em&gt;
&lt;p&gt;
Please note that a line without a newline terminator can only be when the line is the last line of a file, and the line ends with the end of file. In these cases the &lt;tt&gt;read&lt;/tt&gt; command will return false. This is described in the bash manual. Check &lt;tt&gt;man read&lt;/tt&gt; scroll to the read section. Or have a look at here: &lt;a href=&quot;http://ss64.com/bash/read.html&quot; rel=&quot;nofollow&quot;&gt;http://ss64.com/bash/read.html&lt;/a&gt; . This says
&lt;blockquote&gt;
The  return  code  is zero, unless end-of-file is encountered, read times out, or an invalid  file  descriptor is supplied as the argument to -u.
&lt;/blockquote&gt;
Shoot a text editor and write a small line and do not enter a new line after it, save and close. Reopen and recheck if the editor inserted the newlines automatically, if yes then remove them and resave, then try reading this with &lt;tt&gt;read&lt;/tt&gt; it would return false (1), but the line would be read in the variable.
&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;&lt;em&gt;&quot;I decided to test this and put in some echo lines in the [ ! -z &quot;$line&quot; ] test. I found that the code worked fine without it.&quot;&lt;/em&gt;
&lt;p&gt;
In the version of the code originally posted in the blog, because for those files of which the last lines are directly terminated with an end-of-file , &lt;tt&gt;read&lt;/tt&gt; returns false, so the body of the while loop does not gets executed. But this last line is still in the variable, and the &lt;tt&gt;[ ! -z &quot;$line&quot; ]&lt;/tt&gt; will become true and this last line is manually assigned to the array. This way we consider all the lines in the file. In case the last line did end with a new line, then at the last iteration the &lt;tt&gt;line&lt;/tt&gt; variable contains null, so the condition becomes false and we do not enter it.
&lt;/p&gt;

&lt;li&gt;&lt;em&gt;eliminate the while read loop altogether, including the test of the last line.&lt;/em&gt;&lt;/li&gt;
&lt;p&gt;
This is true. I actually wanted to not use this method. Actually there are even more methods, like open the file for reading with some file descriptor with &lt;tt&gt;exec&lt;/tt&gt; and use that descriptor with &lt;tt&gt;read -u&lt;/tt&gt;. The way you have done is a good way, and the last line is included which does not depend wether it ends with a new line or an eof. But still you need to set the IFS to partition the contents of the file at the newline characters.
&lt;/p&gt;
&lt;ol&gt;

The case in which your code will fail is:
&lt;ol&gt;
&lt;li&gt; If you have a file say with two lines as folows:
[sourcecode language=&quot;text&quot;]
l1
l2[/sourcecode]

where there is no newline after the line 2 , ie. &quot;l2&quot; ends directly with eof.
Your code would generate output
[sourcecode language=&quot;text&quot;]
l2
l1
[/sourcecode]
where the output should be
[sourcecode language=&quot;text&quot;]
l2l1
[/sourcecode]
because there is no newline after l2. Compare output with the program &lt;tt&gt;tac&lt;/tt&gt; which performs the same job as the script.
&lt;/li&gt;
&lt;/ol&gt;

Where both of our code fails is in:
&lt;ol&gt;
&lt;li&gt;create a file containing the contents:
[sourcecode language=&quot;text&quot;]
h\ne\nl\nl\no
[/sourcecode]

both of our code will output

[sourcecode language=&quot;text&quot;]
h
e
l
l
o
[/sourcecode]

where the output should be

[sourcecode language=&quot;text&quot;]
h\ne\nl\nl\no
[/sourcecode]
&lt;/li&gt;
&lt;/ol&gt;

So at the end we have come to know that this thing can be done in a lot of ways. The best thing is that i have found one more bug in my code when replying to your comment. This is same as after &quot;@DangerousDaren&#039;s&quot; comment i found one and eliminated.

Thank you for going through the code and commeting with such details. Please let me know if you have any comments or other possibilities. Also i would request you to have a look at the bugfixed version of my code, and the updated description text.]]></description>
		<content:encoded><![CDATA[<ol>
<li><em>&#8220;is it really necessary to set IFS=$’\n’&#8221;</em>
<p>
Yes it is necessary to set this. Because the default IFS characters seperate the fields of by the blankspace and the tab and the new line. So if you read one single line into the array like that with the default IFS value then you will end up with one single word in each array location. For example if file has a line : <tt>hello man this is s test</tt> and you execute the <tt>arr=( $( &lt; $file_name ) )</tt> , then <tt>arr[0]</tt> will be &#8220;hello&#8221;, <tt>arr[1]</tt> will be &#8220;man&#8221; etc. You can test the result by not setting the <tt>IFS=$'\n'</tt>. Setting this makes the field seperator only the new lines. (Note that the code version you have posted in the comment contains the <tt>IFS=$'\n'</tt> line.
</p>
</li>
<li><em>&#8220;Following on from your assumption that the ‘while read -r line’ test evaluated false if there was no newline,&#8221;</em>
<p>
Please note that a line without a newline terminator can only be when the line is the last line of a file, and the line ends with the end of file. In these cases the <tt>read</tt> command will return false. This is described in the bash manual. Check <tt>man read</tt> scroll to the read section. Or have a look at here: <a href="http://ss64.com/bash/read.html" rel="nofollow">http://ss64.com/bash/read.html</a> . This says</p>
<blockquote><p>
The  return  code  is zero, unless end-of-file is encountered, read times out, or an invalid  file  descriptor is supplied as the argument to -u.
</p></blockquote>
<p>Shoot a text editor and write a small line and do not enter a new line after it, save and close. Reopen and recheck if the editor inserted the newlines automatically, if yes then remove them and resave, then try reading this with <tt>read</tt> it would return false (1), but the line would be read in the variable.
</p>
</li>
<li><em>&#8220;I decided to test this and put in some echo lines in the [ ! -z "$line" ] test. I found that the code worked fine without it.&#8221;</em>
<p>
In the version of the code originally posted in the blog, because for those files of which the last lines are directly terminated with an end-of-file , <tt>read</tt> returns false, so the body of the while loop does not gets executed. But this last line is still in the variable, and the <tt>[ ! -z "$line" ]</tt> will become true and this last line is manually assigned to the array. This way we consider all the lines in the file. In case the last line did end with a new line, then at the last iteration the <tt>line</tt> variable contains null, so the condition becomes false and we do not enter it.
</p>
</li>
<li><em>eliminate the while read loop altogether, including the test of the last line.</em></li>
<p>
This is true. I actually wanted to not use this method. Actually there are even more methods, like open the file for reading with some file descriptor with <tt>exec</tt> and use that descriptor with <tt>read -u</tt>. The way you have done is a good way, and the last line is included which does not depend wether it ends with a new line or an eof. But still you need to set the IFS to partition the contents of the file at the newline characters.
</p>
</ol>
<ol>
<p>The case in which your code will fail is:
</ol>
<ol>
<li> If you have a file say with two lines as folows:
<pre class="brush: plain;">
l1
l2</pre>
<p>where there is no newline after the line 2 , ie. &#8220;l2&#8243; ends directly with eof.<br />
Your code would generate output</p>
<pre class="brush: plain;">
l2
l1
</pre>
<p>where the output should be</p>
<pre class="brush: plain;">
l2l1
</pre>
<p>because there is no newline after l2. Compare output with the program <tt>tac</tt> which performs the same job as the script.
</li>
</ol>
<p>Where both of our code fails is in:</p>
<ol>
<li>create a file containing the contents:
<pre class="brush: plain;">
h\ne\nl\nl\no
</pre>
<p>both of our code will output</p>
<pre class="brush: plain;">
h
e
l
l
o
</pre>
<p>where the output should be</p>
<pre class="brush: plain;">
h\ne\nl\nl\no
</pre>
</li>
</ol>
<p>So at the end we have come to know that this thing can be done in a lot of ways. The best thing is that i have found one more bug in my code when replying to your comment. This is same as after &#8220;@DangerousDaren&#8217;s&#8221; comment i found one and eliminated.</p>
<p>Thank you for going through the code and commeting with such details. Please let me know if you have any comments or other possibilities. Also i would request you to have a look at the bugfixed version of my code, and the updated description text.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AngryCoder</title>
		<link>http://phoxis.org/2010/03/04/bash-print-lines-reversed/#comment-1803</link>
		<dc:creator><![CDATA[AngryCoder]]></dc:creator>
		<pubDate>Fri, 13 May 2011 12:50:29 +0000</pubDate>
		<guid isPermaLink="false">http://phoxis.wordpress.com/?p=847#comment-1803</guid>
		<description><![CDATA[Hi, 
Nice blog post! I googled upon this as I was doing some &#039;bash&#039;ing :-)
I have some observations as been playing around with this a bit, as I was contemplating your terminal conditions. 
As you say &#039;All these stuff, to handle one terminal condition&#039;, this made me think if it weren&#039;t possible to eliminate the cause of the condition (i.e. the absense or not of the final newline). 
So my first question was, is it really necessary to set IFS=$&#039;\n&#039;? Following on from your assumption that the &#039;while read -r line&#039; test evaluated false if there was no newline, I decided to test this and put in some echo lines in the [ ! -z &quot;$line&quot; ] test. I found that the code worked fine without it. In fact I found that it made no difference whatsoever, with or without it. This could be due to the fact that I only did limited testing with a file containing 5 lines, no escape characters but with and without a terminating newline. 
So even if my input file did not have a terminating newline, the script would still process fine (and the last $line would always be empty).
I tested this both on an Ubuntu 10.10 and a Mac OS X 10.6.7 machine.
Digging a lil further I found that bash has a special construct that enables the reading of a file into an array. That way you can eliminate the while read loop altogether, including the test of the last line.
So with my (albeit limited) testing, you could do the same with this somewhat smaller piece of code:
[sourcecode language=&quot;bash&quot;]
#!/bin/bash
#This code is a part of http://phoxis.wordpress.com
#

file_name=&quot;$1&quot;
     #Check if file name is given, and it exists

if [ &quot;$#&quot; -eq 0 ]
  then
   echo &quot;Syntax: $0 filename&quot;
   exit
elif [ ! -f &quot;$file_name&quot; ]
  then
     echo &quot;File \&quot;&quot;$file_name&quot;\&quot; does not exist&quot;
     exit
fi

     #Set the IFS variable to \n this enables reading one \n separated
     # line per read

IFS=$&#039;\n&#039;

     #Read from file_name and store contents into array.
arr=( $( &lt; $file_name ) )

     #Count number of lines, and adjust index

i=${#arr[*]}
i=$((i-1))

     #Print the lines in reverse order
while [ $i -ge 0 ]
do
   echo -e &quot;${arr[$i]}&quot;
   i=$((i-1))
done
[/sourcecode]

Note that I&#039;m a bash n00b who&#039;s trying to learn more so if I&#039;m off on my conclusions (very well possible due to my said limited testing), please correct me.

Cheers!]]></description>
		<content:encoded><![CDATA[<p>Hi,<br />
Nice blog post! I googled upon this as I was doing some &#8216;bash&#8217;ing :-)<br />
I have some observations as been playing around with this a bit, as I was contemplating your terminal conditions.<br />
As you say &#8216;All these stuff, to handle one terminal condition&#8217;, this made me think if it weren&#8217;t possible to eliminate the cause of the condition (i.e. the absense or not of the final newline).<br />
So my first question was, is it really necessary to set IFS=$&#8217;\n&#8217;? Following on from your assumption that the &#8216;while read -r line&#8217; test evaluated false if there was no newline, I decided to test this and put in some echo lines in the [ ! -z "$line" ] test. I found that the code worked fine without it. In fact I found that it made no difference whatsoever, with or without it. This could be due to the fact that I only did limited testing with a file containing 5 lines, no escape characters but with and without a terminating newline.<br />
So even if my input file did not have a terminating newline, the script would still process fine (and the last $line would always be empty).<br />
I tested this both on an Ubuntu 10.10 and a Mac OS X 10.6.7 machine.<br />
Digging a lil further I found that bash has a special construct that enables the reading of a file into an array. That way you can eliminate the while read loop altogether, including the test of the last line.<br />
So with my (albeit limited) testing, you could do the same with this somewhat smaller piece of code:</p>
<pre class="brush: bash;">
#!/bin/bash
#This code is a part of http://phoxis.wordpress.com
#

file_name=&quot;$1&quot;
     #Check if file name is given, and it exists

if [ &quot;$#&quot; -eq 0 ]
  then
   echo &quot;Syntax: $0 filename&quot;
   exit
elif [ ! -f &quot;$file_name&quot; ]
  then
     echo &quot;File \&quot;&quot;$file_name&quot;\&quot; does not exist&quot;
     exit
fi

     #Set the IFS variable to \n this enables reading one \n separated
     # line per read

IFS=$'\n'

     #Read from file_name and store contents into array.
arr=( $( &lt; $file_name ) )

     #Count number of lines, and adjust index

i=${#arr[*]}
i=$((i-1))

     #Print the lines in reverse order
while [ $i -ge 0 ]
do
   echo -e &quot;${arr[$i]}&quot;
   i=$((i-1))
done
</pre>
<p>Note that I&#8217;m a bash n00b who&#8217;s trying to learn more so if I&#8217;m off on my conclusions (very well possible due to my said limited testing), please correct me.</p>
<p>Cheers!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: phoxis</title>
		<link>http://phoxis.org/2010/03/04/bash-print-lines-reversed/#comment-1596</link>
		<dc:creator><![CDATA[phoxis]]></dc:creator>
		<pubDate>Wed, 16 Mar 2011 01:51:03 +0000</pubDate>
		<guid isPermaLink="false">http://phoxis.wordpress.com/?p=847#comment-1596</guid>
		<description><![CDATA[Thanks for getting the bug killed. The only known issue with the code is that when printing the contents of a file not ending with a newline, the last line and the last but one line are printed in separate lines, which should not be. I have made another update, files having the last line without a newline are printed correctly. In the previous version the null terminated last line and the last but one line was printed in two lines (which is incorrect), but now they are printed adjacent (as there is no \n in between the two lines) .

All these stuff, to handle one terminal condition :) , but you can never tell when the overlooked terminal conditions become vital in your application.]]></description>
		<content:encoded><![CDATA[<p>Thanks for getting the bug killed. The only known issue with the code is that when printing the contents of a file not ending with a newline, the last line and the last but one line are printed in separate lines, which should not be. I have made another update, files having the last line without a newline are printed correctly. In the previous version the null terminated last line and the last but one line was printed in two lines (which is incorrect), but now they are printed adjacent (as there is no \n in between the two lines) .</p>
<p>All these stuff, to handle one terminal condition :) , but you can never tell when the overlooked terminal conditions become vital in your application.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DangerousDaren</title>
		<link>http://phoxis.org/2010/03/04/bash-print-lines-reversed/#comment-1595</link>
		<dc:creator><![CDATA[DangerousDaren]]></dc:creator>
		<pubDate>Tue, 15 Mar 2011 20:30:28 +0000</pubDate>
		<guid isPermaLink="false">http://phoxis.wordpress.com/?p=847#comment-1595</guid>
		<description><![CDATA[Phoxis,

You are indeed correct,  I guess &quot;two wrongs do make a right&quot; in this case ;-)]]></description>
		<content:encoded><![CDATA[<p>Phoxis,</p>
<p>You are indeed correct,  I guess &#8220;two wrongs do make a right&#8221; in this case ;-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: phoxis</title>
		<link>http://phoxis.org/2010/03/04/bash-print-lines-reversed/#comment-1593</link>
		<dc:creator><![CDATA[phoxis]]></dc:creator>
		<pubDate>Tue, 15 Mar 2011 05:07:14 +0000</pubDate>
		<guid isPermaLink="false">http://phoxis.wordpress.com/?p=847#comment-1593</guid>
		<description><![CDATA[Although it is not a typo but an attempt to stop the last dummy blank line from being printed. But taking a closer look at the code reviled a bug in the code.
Note that there is an extra line arr+=(“$line”) after the loop ends. If the file does end with a newline then this will be considered by the loop, and the extra line would add an extra newline the array which does not need to be printed. For this the index was being decreased by 2.
If the file does not ended with a new line, instead the last line is null terminated, then although the read command would read that line but would return false and the loop will break without entering the read in value into the array. In this case the outside arr+=(“$line”) would do the entry. And in this case the last line is important to print so in this case the array adjustment needs to be decreased by 1.
After your comment i had a closer look at the code, and edited the code that if the last line read in was terminated with a newline then after the loop breaks the $line would contain an empty string, in which case it should not be entered in the array. And if the file ends with a null, then after the loop breaks $line will have the last line which was read in. This line was not entered in the while loop (as told above) and should be added manually to the array. After this conditional last line insertion is done, before printing the array index adjustment needed is only i=$((i-1)) .
The code is changed as per this change.]]></description>
		<content:encoded><![CDATA[<p>Although it is not a typo but an attempt to stop the last dummy blank line from being printed. But taking a closer look at the code reviled a bug in the code.<br />
Note that there is an extra line arr+=(“$line”) after the loop ends. If the file does end with a newline then this will be considered by the loop, and the extra line would add an extra newline the array which does not need to be printed. For this the index was being decreased by 2.<br />
If the file does not ended with a new line, instead the last line is null terminated, then although the read command would read that line but would return false and the loop will break without entering the read in value into the array. In this case the outside arr+=(“$line”) would do the entry. And in this case the last line is important to print so in this case the array adjustment needs to be decreased by 1.<br />
After your comment i had a closer look at the code, and edited the code that if the last line read in was terminated with a newline then after the loop breaks the $line would contain an empty string, in which case it should not be entered in the array. And if the file ends with a null, then after the loop breaks $line will have the last line which was read in. This line was not entered in the while loop (as told above) and should be added manually to the array. After this conditional last line insertion is done, before printing the array index adjustment needed is only i=$((i-1)) .<br />
The code is changed as per this change.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DangerousDaren</title>
		<link>http://phoxis.org/2010/03/04/bash-print-lines-reversed/#comment-1591</link>
		<dc:creator><![CDATA[DangerousDaren]]></dc:creator>
		<pubDate>Tue, 15 Mar 2011 00:47:25 +0000</pubDate>
		<guid isPermaLink="false">http://phoxis.wordpress.com/?p=847#comment-1591</guid>
		<description><![CDATA[I think you&#039;ll find that you have a typo error in line #37 where you are adjusting for zero based array indexing:

i=$(($i-2))    should read   i=$(($i-1))]]></description>
		<content:encoded><![CDATA[<p>I think you&#8217;ll find that you have a typo error in line #37 where you are adjusting for zero based array indexing:</p>
<p>i=$(($i-2))    should read   i=$(($i-1))</p>
]]></content:encoded>
	</item>
</channel>
</rss>

