Linux 如何更改grep查看文件/打印结果的顺序?
我有一个目录,里面有一堆带有数字文件名的文件。它们没有前导零,因此如果我在该目录中执行类似于Linux 如何更改grep查看文件/打印结果的顺序?,linux,bash,grep,gnu,Linux,Bash,Grep,Gnu,我有一个目录,里面有一堆带有数字文件名的文件。它们没有前导零,因此如果我在该目录中执行类似于grep hello*的操作,可能会得到如下结果: 22:hello, world! 6:hello 62:"Say hello to them for me." 6:hello 22:hello, world! 62:"Say hello to them for me." 我宁愿结果是这样的: 22:hello, world! 6:hello 62:"Say hello to them for me
grep hello*
的操作,可能会得到如下结果:
22:hello, world!
6:hello
62:"Say hello to them for me."
6:hello
22:hello, world!
62:"Say hello to them for me."
我宁愿结果是这样的:
22:hello, world!
6:hello
62:"Say hello to them for me."
6:hello
22:hello, world!
62:"Say hello to them for me."
我想到的第一个想法是用
grep hello*|sort-n
对结果进行数字排序,但随后我丢失了grep的颜色,我想保留它。最好的方法是什么?谢谢!因此,xargs命令所做的就是接受标准输入,用换行符将其拆分,然后将结果作为参数附加到“grep hello”中,然后?@pandubear是的,没错。说得再好不过了。您还可以将-H
选项添加到grep
以打印每个匹配项的文件名(以获得与grep-R
相同的输出)
ls * | sort -n | xargs -d '\n' grep hello