Linux 用 grep 匹配一行中的两个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4487328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Match two strings in one line with grep
提问by hearsaxas
I am trying to use grep
to match lines that contain two different strings. I have tried the following but this matches lines that contain either string1orstring2which not what I want.
我试图用来grep
匹配包含两个不同字符串的行。我尝试了以下操作,但这匹配包含string1或string2 的行,这不是我想要的。
grep 'string1\|string2' filename
So how do I match with grep
only the lines that contain both strings?
那么如何grep
只匹配包含两个字符串的行呢?
采纳答案by dheerosaur
You can use grep 'string1' filename | grep 'string2'
您可以使用 grep 'string1' filename | grep 'string2'
Or, grep 'string1.*string2\|string2.*string1' filename
或者, grep 'string1.*string2\|string2.*string1' filename
回答by martineno
The |
operator in a regular expression means or. That is to say either string1 or string2 will match. You could do:
|
正则表达式中的运算符表示或。也就是说 string1 或 string2 将匹配。你可以这样做:
grep 'string1' filename | grep 'string2'
which will pipe the results from the first command into the second grep. That should give you only lines that match both.
这会将第一个命令的结果通过管道传输到第二个 grep 中。那应该只给你匹配两者的行。
回答by Dorn
You could try something like this:
你可以尝试这样的事情:
(pattern1.*pattern2|pattern2.*pattern1)
回答by Raghuram
You should have grep
like this:
你应该有grep
这样的:
$ grep 'string1' file | grep 'string2'
回答by tchrist
If you have a grep
with a -P
option for a limited perl
regex, you can use
如果你有一个有限正则表达式grep
的-P
选项perl
,你可以使用
grep -P '(?=.*string1)(?=.*string2)'
which has the advantage of working with overlapping strings. It's somewhat more straightforward using perl
as grep
, because you can specify the and logic more directly:
这具有处理重叠字符串的优点。使用perl
as更直接一些grep
,因为您可以更直接地指定 and 逻辑:
perl -ne 'print if /string1/ && /string2/'
回答by user45949
I think this is what you were looking for:
我想这就是你要找的:
grep -E "string1|string2" filename
I think that answers like this:
我认为答案是这样的:
grep 'string1.*string2\|string2.*string1' filename
only match the case where both are present, not one or the other or both.
只匹配两者都存在的情况,而不是一个或另一个或两者都存在的情况。
回答by Aquarius Power
for multiline match:
对于多行匹配:
echo -e "test1\ntest2\ntest3" |tr -d '\n' |grep "test1.*test3"
or
或者
echo -e "test1\ntest5\ntest3" >tst.txt
cat tst.txt |tr -d '\n' |grep "test1.*test3\|test3.*test1"
we just need to remove the newline character and it works!
我们只需要删除换行符就可以了!
回答by Kinjal Dixit
To search for files containing all the words in any order anywhere:
要在任何地方以任何顺序搜索包含所有单词的文件:
grep -ril \'action\' | xargs grep -il \'model\' | xargs grep -il \'view_type\'
The first grep kicks off a recursive search (r
), ignoring case (i
) and listing (printing out) the name of the files that are matching (l
) for one term ('action'
with the single quotes) occurring anywhere in the file.
第一个 grep 启动递归搜索 ( r
),忽略大小写 ( i
) 并列出(打印出)与文件中出现l
的某个术语('action'
带单引号)匹配 ( )的文件的名称。
The subsequent greps search for the other terms, retaining case insensitivity and listing out the matching files.
随后的 grep 搜索其他术语,保留不区分大小写并列出匹配的文件。
The final list of files that you will get will the ones that contain these terms, in any order anywhere in the file.
您将获得的最终文件列表将包含这些术语的文件,以文件中任何位置的任何顺序排列。
回答by Leo
Your method was almost good, only missing the -w
你的方法几乎很好,只是缺少 -w
grep -w 'string1\|string2' filename
回答by Tim Seed
Place the strings you want to grep for into a file
将您想要 grep 的字符串放入文件中
echo who > find.txt
echo Roger >> find.txt
echo [44][0-9]{9,} >> find.txt
Then search using -f
然后使用 -f 搜索
grep -f find.txt BIG_FILE_TO_SEARCH.txt