filter to reverse lines of a text file

Posted by Greg Hewgill on Stack Overflow See other posts from Stack Overflow or by Greg Hewgill
Published on 2009-01-06T21:38:25Z Indexed on 2010/05/06 2:48 UTC
Read the original article Hit count: 319

Filed under:
|
|

I'm writing a small shell script that needs to reverse the lines of a text file. Is there a standard filter command to do this sort of thing?

My specific application is that I'm getting a list of Git commit identifiers, and I want to process them in reverse order:

git log --pretty=oneline work...master | grep -v DEBUG: | cut -d' ' -f1 | reverse

The best I've come up with is to implement reverse like this:

... | cat -b | sort -rn | cut -f2-

This uses cat to number every line, then sort to sort them in descending numeric order (which ends up reversing the whole file), then cut to remove the unneeded line number.

The above works for my application, but may fail in the general case because cat -b only numbers nonblank lines.

Is there a better, more general way to do this?

© Stack Overflow or respective owner

Related posts about shell

Related posts about filter