bash 如何更改 grep 查看文件/打印结果的顺序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17100244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:41:19  来源:igfitidea点击:

How do I change the order in which grep looks at files/prints results?

linuxbashgrepgnu

提问by pandubear

I have a directory with a bunch of files with numerical filenames. They don't have leading zeroes, so if I do something like grep hello *in that directory I might get something like this:

我有一个目录,里面有一堆带有数字文件名的文件。他们没有前导零,所以如果我grep hello *在那个目录中做类似的事情,我可能会得到这样的东西:

22:hello, world!
6:hello
62:"Say hello to them for me."

I'd rather have the result be like this:

我宁愿结果是这样的:

6:hello
22:hello, world!
62:"Say hello to them for me."

The first thought that occured to me was to sort the results numerically with grep hello * | sort -nbut then I lose grep's colors, which I'd like to keep. What's the best way to do that?

我想到的第一个想法是用数字对结果进行排序,grep hello * | sort -n但随后我丢失了 grep 的颜色,我想保留它。最好的方法是什么?

回答by John Kugelman

ls * | sort -n | xargs -d '\n' grep hello