sorting and reversing lines of a file

Jon H. LaBadie jon at jonlab.UUCP
Mon Jan 30 16:03:28 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.
>     E.g., if the file "f" contains
>        line1 
>        line2
>        line3
>     I want to produce
>        line 3
>        line 2
>        line 1
> 
Try the global command of ed(1), or ex/vi(1).  If vi is your interactive
editor, just type (from anywhere in the file):

	:g/^/m0

This globally finds every line with a beginning, and one at a time,
moves them to the beginning of the file.  Because of the speed, I'm
sure the data is not reorganized until it is written back to the file.

If you need to do this non-interactively, try a shell script invoking
ed with a "here document".  Something like this should work.

	ed - ${1:?"Need a file name"} <<-!
		g/^/m0
		w
		q
	!

Put it in a file called flip, and flip those lines.
-- 
Jon LaBadie
{att, ulysses, princeton, bcr}!jonlab!jon



More information about the Comp.unix.questions mailing list