[olug] Unix Tip: OUTPUT A FILE IN REVERSE

Daniel G. Linder dlinder at iprevolution.com
Thu Apr 24 01:15:12 UTC 2003


William E. Kempf wrote:
> My thoughts from this tip, and in this order:
>
> 1) Where do *nix programmers go to learn how to name things?

If I remember correctly, the "cat" command name came from an old machine
code command...or is that the LISP functions? :)
                                                                                
> 2) Why would I ever want to list out a text file in reverse line order?
> (The few corner cases I can think of would involve heavy file manipulation
> where I'd likely right a more sophisticated program/script that would be
> more efficient than using 'tac' in a pipe.)
                                                                                
Maybe you want to view a log file with the most recent things "first"...
                                                                                
> 3) Why in the world is this a seperate program?  (If there is a need for
> this sort of thing, shouldn't it be a command line switch on cat?)
I agree -- on my RH9 system, they are both supplied from the "coreutils-4.5.3-19" RPM...  I would bet 75% of the code is identical and could be re-used with a simple check as to how the program was invoked.
                                                                                
> The last question would immediately solve the '-n' observation above...
> though I'd have to question whether or not the line numbers should be
> reversed as well in this case?
                                                                                
How about this?
Original file:
[dan at localhost dan]$ cat /tmp/cat.file
Line 1
Line 2
Line 3
Line 4
Line 5
                                                                                
Numbers added, then the file is reversed.
[dan at localhost dan]$ cat -n /tmp/cat.file | tac
     5  Line 5
     4  Line 4
     3  Line 3
     2  Line 2
     1  Line 1
                                                                                
File reversed, then lines numbered.
[dan at localhost dan]$ tac /tmp/cat.file | cat -n
     1  Line 5
     2  Line 4
     3  Line 3
     4  Line 2
     5  Line 1
                                                                                
Piping is a wonderfull thing!  Just try doing THAT with a GUI interface!  [And don't brining up SmallTalk... :)]
                                                                                
Dan



More information about the OLUG mailing list