grep -f文件以文件顺序打印 [英] grep -f file to print in order as a file
问题描述
我需要从文件中获取grep模式,但需要按顺序排列.
I have a a requirement to grep patterns from a file but need them in order.
$ cat patt.grep
name1
name2
$ grep -f patt.grep myfile.log
name2:some xxxxxxxxxx
name1:some xxxxxxxxxx
我得到的输出是,首先找到了name2,然后将其打印出来,然后找到了name1,也将其打印出来.但是我的要求是按照patt.grep文件的顺序先获取name1.
I am getting the output as name2 was found first it was printed then name1 is found it is also printed. But my requirement is to get the name1 first as per the order of patt.grep file.
我期望输出为
name1:some xxxxxxxxxx
name2:some xxxxxxxxxx
推荐答案
您可以将patt.grep
用管道传输到xargs
,这会将模式一次传递给grep
.
You can pipe patt.grep
to xargs
, which will pass the patterns to grep
one at a time.
默认情况下,xargs
在命令末尾附加参数.但是在这种情况下,grep
需要myfile.log
作为最后一个参数.因此,使用-I{}
选项告诉xargs
用参数替换{}
.
By default xargs
appends arguments at the end of the command. But in this case, grep
needs myfile.log
to be the last argument. So use the -I{}
option to tell xargs
to replace {}
with the arguments.
cat patt.grep | xargs -Ihello grep hello myfile.log
这篇关于grep -f文件以文件顺序打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!