grep -f文件以文件顺序打印 [英] grep -f file to print in order as a file

查看:334
本文介绍了grep -f文件以文件顺序打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从文件中获取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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆