grep 匹配与文件名太接近

grep match is too close from filename

我正在使用 grep 来查找值。但是,当该值带有负号时,grep 匹配结果与文件名太接近。

这是查找值的代码:

grep -r "something" * | sort | awk '{print }')

这没用。然后我用了:

grep -r "something" * | sort

此命令的输出是:

filename1:-1.0 "other line contents"  
filename2:-1.0 "other line contents"  
filename3: 1.0 "other line contents"  
filename4: 1.0 "other line contents"

我需要按名称对文件进行排序。

如何在"filename:"之后增加space?还是有别的办法?

你可以使用 cut:

grep -r "something" * | sort | cut -d: -f2-

cut -d: -f2- : 上削减 并为您提供从第二个开始的所有字段。

例如 echo foo:bar:qux | cut -d: -f2- 给出 bar:qux。在你的情况下,它会删除文件名并给你剩下的。