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

2019-01-03 00:37发布

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?

20条回答
迷人小祖宗
2楼-- · 2019-01-03 01:23
tac <file_name>

example:

$ cat file1.txt
1
2
3
4
5

$ tac file1.txt
5
4
3
2
1
查看更多
Anthone
3楼-- · 2019-01-03 01:26

If you happen to be in vim use

:g/^/m0
查看更多
登录 后发表回答