Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reverse the order of lines in a file?

I'd like to reverse the order of lines in a text file (or stdin), preserving the contents of each line.

So, i.e., starting with:

foo bar baz 

I'd like to end up with

baz bar foo 

Is there a standard UNIX commandline utility for this?

like image 841
Scotty Allen Avatar asked Apr 12 '09 21:04

Scotty Allen


People also ask

How do I reverse the order of lines in Vim?

command! -bar -range=% Reverse <line1>,<line2>g/^/m0|nohl " REVERSE line ordering, and move those lines to the top of the file.

Which command is used to sort the lines of data in a file in reverse order?

the -r flag is an option of the sort command which sorts the input file in reverse order i.e. descending order by default.


1 Answers

Also worth mentioning: tac (the, ahem, reverse of cat). Part of coreutils.

Flipping one file into another

tac a.txt > b.txt 
like image 74
Mihai Limbășan Avatar answered Oct 04 '22 11:10

Mihai Limbășan