如何反转文本文件的行?

发布于 2024-07-11 12:10:54 字数 544 浏览 8 评论 0原文

我正在编写一个小型 shell 脚本,需要反转文本文件的行。 是否有标准的过滤器命令可以执行此类操作?

我的具体应用是,我正在获取 Git 提交标识符的列表,并且我想以相反的顺序处理它们:

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

我想出的最好的方法是像这样实现 reverse

... | cat -b | sort -rn | cut -f2-

这使用cat 对每一行进行编号,然后 sort 按降序数字顺序对它们进行排序(最终反转整个文件),然后 cut 删除不需要的行号。

上面的方法适用于我的应用程序,但在一般情况下可能会失败,因为 cat -b 仅对非空行进行编号。

有没有更好、更通用的方法来做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

终难遇 2024-07-18 12:10:54

有一个命令可以满足您的目的:

tail -r file.txt
  • 以相反的顺序打印 file.txt 的行!
  • -r 标志是非标准的,可能无法在所有系统上工作,但可以在 macOS 等系统上工作。
  • 请注意:线路数量有限。 大多数情况下都有效,但在处理大文件时要小心并检查。

There is a command for your purpose:

tail -r file.txt
  • Prints the lines of file.txt in reverse order!
  • The -r flag is non-standard, may not work on all systems, works e.g. on macOS.
  • Beware: Amount of lines limited. Works mostly, but when working with huge files be careful and check.
七秒鱼° 2024-07-18 12:10:54

答案不是 42,而是 tac

编辑:使用sed速度较慢但消耗更多内存

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

,甚至更长

perl -e'print reverse<>'

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<>'
莫多说 2024-07-18 12:10:54

与上面的 sed 示例类似,使用 perl - 也许更容易记住(取决于你的大脑如何连接):

perl -e 'print reverse <>'

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

perl -e 'print reverse <>'
空城缀染半城烟沙 2024-07-18 12:10:54

cat -b 只对非空行进行编号”

如果这是您想要避免的唯一问题,那么为什么不使用“cat -n”对所有行进行编号呢?

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?

网白 2024-07-18 12:10:54

在这种情况下,只需使用 --reverse

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

In this case, just use --reverse:

$ git log --reverse --pretty=oneline work...master | grep -v DEBUG: | cut -d' ' -f1
如歌彻婉言 2024-07-18 12:10:54
:   "@(#)$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]*://'

对我来说就像一个魅力......

:   "@(#)$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...

音盲 2024-07-18 12:10:54
awk '{a[i++]=$0}END{for(;i-->0;)print a[i]}'

sed 更快,并且兼容 openwrt 等嵌入式设备。

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

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

薆情海 2024-07-18 12:10:54
rev <name of your text file.txt>

你甚至可以这样做:

echo <whatever you want to type>|rev
rev <name of your text file.txt>

You can even do this:

echo <whatever you want to type>|rev
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文