How to reverse lines of a text file?

旧街凉风 提交于 2019-11-29 16:09:28

问题


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?


回答1:


In GNU coreutils, there's tac(1)




回答2:


There is a standard command for your purpose:

tail -r file.txt

prints the lines of file.txt in reverse order!




回答3:


Answer is not 42 but tac.

Edit: Slower but more memory consuming using sed

sed 'x;1!H;$!d;x'

and even longer

perl -e'print reverse<>'



回答4:


cat -b only numbers nonblank lines"


If that's the only issue you want to avoid, then why not use "cat -n" to number all the lines?




回答5:


:   "@(#)$Id: reverse.sh,v 1.2 1997/06/02 21:45:00 johnl Exp $"
#
#   Reverse the order of the lines in each file

awk ' { printf("%d:%s\n", NR, $0);}' $* |
sort -t: +0nr -1 |
sed 's/^[0-9][0-9]*://'

Works like a charm for me...




回答6:


Similar to the sed example above, using perl - maybe more memorable (depending on how your brain is wired):

perl -e 'print reverse <>'



回答7:


In this case, just use --reverse:

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



回答8:


rev <name of your text file.txt>

You can even do this:

echo <whatever you want to type>|rev



回答9:


awk '{a[i++]=$0}END{for(;i-->0;)print a[i]}'

More faster than sed and compatible for embed devices like openwrt.



来源:https://stackoverflow.com/questions/418280/how-to-reverse-lines-of-a-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!