如何颠倒文本文件的行? [英] How to reverse lines of a text file?

查看:701
本文介绍了如何颠倒文本文件的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个小的shell脚本,需要翻转文本文件的行。是否有一个标准的过滤器命令来做这种事情?



我的具体应用程序是,我得到一个Git提交标识符列表,我想处理他们以相反的顺序:

  git log --pretty = grep -v DEBUG:| cut -d-f1 |反向

我想到的最好的是实现 reverse 像这样:

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

这使用 cat ,然后 sort 以递减的数字顺序(最后颠倒整个文件)对它们进行排序,然后 cut 删除不需要的行号。



上面的代码适用于我的应用程序,但在一般情况下可能会失败,因为 cat -b

解决方案

GNU coreutils 中,有 tac(1)


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?

解决方案

In GNU coreutils, there's tac(1)

这篇关于如何颠倒文本文件的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆