如何将两个搜索词与"grep"组合(和) [英] How to combine two search words with "grep" (AND)

查看:229
本文介绍了如何将两个搜索词与"grep"组合(和)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用grep,我可以在本地文件夹的50个文件中找到一个单词,即:

With grep, I can find a word within 50 files in a local folder, i.e.:

grep -i "hello" *.html

但是如何查找包含两个单词的文件?示例:我想查找所有包含单词"hello"和单词"peter"的文件.如何合并两个grep?

But how can I find files that contain TWO words? Example: I would like to find all files, that contain Word "hello" AND word "peter". How can I combine two grep's?

推荐答案

要查看包含两个单词的文件(可能在不同的行上),请使用-lxargs:

To see the files containing both words (possibly on different lines), use -l and xargs:

grep -il "hello" *.html | xargs grep -il "peter"

修改

如果文件名中包含空格,则我们需要多加注意.为此,我们可以对grepxargs使用特殊选项:

If your files have spaces in their names, then we need to be a little more careful. For that we can use special options to grep and xargs:

grep -ilZ "hello" *.html | xargs -0 grep -il "peter"

这篇关于如何将两个搜索词与"grep"组合(和)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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