sorting and reversing lines of a file

Jeff Lo jlo at elan.UUCP
Fri Jan 27 08:52:37 AEST 1989


In article <9056 at burdvax.PRC.Unisys.COM> lang at pearl.PRC.Unisys.COM (Francois-Michel Lang) writes:
>I need utilities to do two things:
>(1) reverse the order of lines in a file
>    but leave the lines themselves intact.
>    The Unix utility does just the opposite of this.
>
>    E.g., if the file "f" contains
>       line1 
>       line2
>       line3
>    I want to produce
>       line 3
>       line 2
>       line 1
	    ^ spaces?

>    I have an awk program to do this,
>    but I'm sure some clever soul out there can do much better.

I assume you meant for
	line1
	line2
	line3
to become
	line3
	line2
	line1

Assuming this is the case, you can use a shell script like this:

#!/bin/sh

for file in $* ; do
	ed - $file << __EOF__
g/^/.m0
w
q
__EOF__
done

This will use "ed" to reverse the order of the lines in each file
passed as an argument to the script. This and many other neat "ed"
tricks are in the quiz program. Try: quiz function ed-command
-- 
Jeff Lo
..!{ames,hplabs,uunet}!elan!jlo
Elan Computer Group, Inc.
(415) 322-2450



More information about the Comp.unix.questions mailing list