Unix nerdery

Oh, how shall I reverse the order of lines in a file?

In Vim:

:g/^/m0

(thanks, Vim manual chapter 12!)

From the shell:

tr "\n" "#" < myfile.txt |rev|tr "#" "\n"|rev

(Watch out for #s in your source file)
(thanks, Lonneke!)

Update: GNU Textutils includes tac (cat backwards…) which reverses the lines of its input.

Leave a Reply