Reversing a file?
John Chambers
jc at minya.UUCP
Sat Oct 14 06:37:59 AEST 1989
In article <MONTNARO.89Oct2224215 at sprite.crd.ge.com>, montnaro at sprite.crd.ge.com (Skip Montanaro) writes:
> Does somebody have an elegant shell script for reversing the lines of a
> file? I've come up with the following short one:
... recursive example deleted ...
> 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.
Here's the fastest way I can think of without stooping to writing in C:
for f
do ed - $f <<'EOF'
g/^/m0
w
q
EOF
done
This takes a list of filenames, and reverses each of them. This might
not be, strictly speaking, an answer to your question, since it uses
ed to do the work, and so it isn't really done by the script. It also
has a minor bug (unwanted output) in the boundary case of a null file.
But it works for files as large as ed can handle.
It's curious that when I used "1,$" instead of "g/^/", it didn't work.
I wonder if this is a general ed failing, or if it's just here.
--
#echo 'Opinions Copyright 1989 by John Chambers; for licensing information contact:'
echo ' John Chambers <{adelie,ima,mit-eddie}!minya!{jc,root}> (617/484-6393)'
echo ''
saying
More information about the Comp.unix.questions
mailing list