grep多个模式并使用匹配模式打印结果

丹尼尔·古尔

我正在寻找grep具有多个值的多个文件。例如grep -f filename.txt /home/* -R
(filename.txt包含多个值)

但是,我需要知道哪个文件包含哪个值。
例如:/home/blah.txt包含value1,/ home / aaa.txt包含value2,依此类推。

有什么建议?

阿瑟雷

您可以使用提供的一些标志来进行此操作grep

您可以尝试以下命令:

grep -R -f filename.txt /home/*将搜索filename.txt文件中的任何模式,并将其与主文件夹中的所有文件进行递归匹配。您将获得如下输出:

#here my filename contains the entries bash and Bingo
$grep -R -f filename.txt /home/*
/home/user/.bash_history:vi .bashrc
/home/user/.bash_history:vi .bashrc
/home/user/.bash_history:awk -v RS=END_WORD '/Bingo1/ && /Bingo2/' test2.txt | grep -o 'Pokeman.*'

grep 将输出文件名和包含模式的完整行。

如果只想显示找到的模式,请尝试以下命令:

#here the -o flag only displays the matched values, and -n adds the line number
$grep -R -o -n -f test /home/*
/home/user/.bash_history:128:bash
/home/user/.bash_history:156:bash
/home/user/.bash_history:193:Bingo

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章