Quantcast
Channel: How can I reverse the order of lines in a file? - Stack Overflow
Viewing all articles
Browse latest Browse all 26

Answer by DerMike for How can I reverse the order of lines in a file?

$
0
0

If you happen to be in vim use

:g/^/m0

Explanation from @Ronopolis below:

g means "do this globally.
^ means "the beginning of a line".
m means "move the line to a new line number. 0 is which line to move to.
0 means "top of the file, before the current line 1".
So: "Find every line that has a beginning, and move it to line number 0."

You find line 1, and move it to the top. Does nothing. Then find line 2 and move it above line 1, to the top of the file. Now find line 3 and move it to the top. Repeat this for every line. At the end you finish by moving the last line to the top. When you are done, you've reversed all the lines.


Viewing all articles
Browse latest Browse all 26

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>