[olug] Unix Tip: OUTPUT A FILE IN REVERSE

Jay Hannah jay at jays.net
Tue Apr 22 21:52:56 UTC 2003



On Tue, 22 Apr 2003, William E. Kempf wrote:
> 1) Where do *nix programmers go to learn how to name things?

My favorites in this genre are "less" and "bash". I like "tac" too ("cat"
backwards). -grin-

> 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.)

We have a process that needs to read lines of data from a text file in
reverse chronological order. Each file is in exact forward chronological
order. tac saves us from having to slurp each file completely into memory
and/or resort it before processing can begin.  Since each pass of our
program needs to read 50-300 specific files from a directory containing
tens of thousands of files, tac saves us a lot of time.

In 10 years I had never needed such a thing. When I did, I was glad to
have it. Haven't used it since. -grin-

> 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?)

-shrug-

> 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?

Take your pick:

tac filename | cat - -n
cat -n filename | tac

Laugh,

j

Omaha Perl Mongers: http://omaha.pm.org






More information about the OLUG mailing list