Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何更改grep查看文件/打印结果的顺序?_Linux_Bash_Grep_Gnu - Fatal编程技术网

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