Reversing a file?

Skip Montanaro montnaro at sprite.crd.ge.com
Tue Oct 3 12:42:15 AEST 1989


Does somebody have an elegant shell script for reversing the lines of a
file? I've come up with the following short one:

----------cut----------cut----------cut----------cut----------
#!/bin/sh

read line

if [ $? = 0 ] ; then
    $0
    echo $line
fi
----------cut----------cut----------cut----------cut----------

It has two obvious disadvantages. First, it won't work for very long files,
since it uses Unix processes to simulate a stack of lines.  Second, the
Bourne shell's builtin read command doesn't preserve interword white space,
separating words by the value of the IFS environment variable instead.  It
worked adequately for the task I originally intended, however - reversing
the lines in my appointements file.
--
Skip Montanaro (montanaro at crdgw1.ge.com)



More information about the Comp.unix.questions mailing list