Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!purdue!bu-cs!mirror!ima!minya!jc From: jc@minya.UUCP (John Chambers) Newsgroups: comp.unix.questions Subject: Re: Reversing a file? Message-ID: <40@minya.UUCP> Date: 13 Oct 89 20:56:07 GMT References: <1989Oct3.201759.19182@mrspoc.Transact.COM> Organization: home Lines: 33 In article <1989Oct3.201759.19182@mrspoc.Transact.COM>, itkin@mrspoc.Transact.COM (Steven M. List) writes: > montnaro@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