Reversing a file?

John Chambers jc at minya.UUCP
Sat Oct 14 06:56:07 AEST 1989


In article <1989Oct3.201759.19182 at mrspoc.Transact.COM>, itkin at mrspoc.Transact.COM (Steven M. List) writes:
> 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:
> 
> This uses one of my all-time favorite VI/EX commands, and this is the
> first time I can remember anyone ASKING for it:
> 	echo "g/./.m0\nw $OUTPUT\nq" | ex $INPUT
> the "g/./.m0" marks every line in the file and then moves each marked
> line to the beginning of the file (after line zero).  

No, it doesn't.  You didn't test it against a file containing null lines.
If you had, you would have discovered that the "g/./" only matches lines
with characters in them.  So what you get is the non-null lines in reverse
order, followed by as many null lines as were in the original file.  Try:
 	echo "g/^/m0\nw $OUTPUT\nq" | ex $INPUT
Note also that the second dot, while not wrong, is not needed.  

(I guess you flunk your ex-wizard test.  Now if I could only figure out
how to type map commands to vi so that it does something useful, rather
than giving me error messages.  ;-)

>						The "w $OUTPUT"
> will either write the reversed file to a new file or overwrite the
> original file, depending on whether or not OUTPUT is valued.

That's clever.

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