Reversing a file?

Richard O'Keefe ok at cs.mu.oz.au
Tue Oct 3 21:43:11 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?

In BSD systems,
	cat -n File
puts six-digit line numbers and a tab in front of every line.
In System V,
	pr -t -n6 File
will do this.
Now sort the lines in descending order of line number
    |	sort -nr
Now you want to throw away the line numbers.  In System V,
    |	cut -f2-
will do the job.  If you haven't got cut(1),
    |	sed -e 's/^.......//'
will strip off the spaces, digits, and tab.  So

In BSD systems:
	cat -n $* | sort -nr | sed -e 's/^.......//'
In System V:
	pr -t -n6 $* | sort -nr | cut -f2-



More information about the Comp.unix.questions mailing list