bash: lines of a file in reverse order?

Lennart Sorensen lsorense-1wCw9BSqJbv44Nm34jS7GywD8/FfD2ys at public.gmane.org
Thu Aug 24 18:36:36 UTC 2006


On Thu, Aug 24, 2006 at 02:29:09PM -0400, Interlug wrote:
> How do I return the contents of a file in reverse order, by line?  
> 
> I have 
> 
> Line 1
> Line 2
> Line 3
> 
> I want it to come back as 
> 
> Line 3
> Line 2 
> Line 1
> 
> I was hoping tail -r would do it, but this appears to be a tail under
> OSX thing. 
> http://www.ss64.com/osx/tail.html
> 
> rev doesn't do it.
> 
> $ rev < lines
> 1 eniL
> 2 eniL
> 3 eniL
> 
> I want to find the "last matching line" in a large log file.  My hope
> was:  
> 
> tail -50 -r filename | grep -m 1 "matching" 
> would grab it for me if it exists in the last 50 lines of the log.  
> 
> Surely this is being done elsewhere.  Got a pointer? 

The reason there doesn't seem to be a tool to do it, is that it can't be
done efficiently without using tons of memory, and hence doesn't fit
into a pipeline.  After all you have to store ALL the lines before you
can start to output anything, since the first output is the last input.
It simply doesn't fit in the way the standard command line tools do,
which almost all work line by line.

Of course this doesn't mean there aren't tools to do it, just that it
isn't something likeot to be efficient.  What you want is 'tac' (that is
cat backwards) :)

--
Len Sorensen
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list