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

pan*_*ear 4 linux bash grep gnu

我有一个目录,其中包含一堆带有数字文件名的文件.它们没有前导零,所以如果我grep hello *在该目录中执行类似的操作,我可能会得到类似这样的内容:

22:hello, world!
6:hello
62:"Say hello to them for me."
Run Code Online (Sandbox Code Playgroud)

我宁愿让结果像这样:

6:hello
22:hello, world!
62:"Say hello to them for me."
Run Code Online (Sandbox Code Playgroud)

发生在我身上的第一个想法是用数字方式对结果进行排序,grep hello * | sort -n但后来我失去了grep的颜色,我想保留它.最好的方法是什么?

Joh*_*ica 6

ls * | sort -n | xargs -d '\n' grep hello
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!那么xargs命令的作用是采用标准输入,用换行符拆分,然后将结果追加到"grep hello"作为参数,然后呢? (3认同)