在同一行上存在2个字的grep [英] grep for 2 words existing on the same line
问题描述
我如何grep包含两行输入的行的行?我正在寻找包含两个词的行,我该怎么做?我试图像这样:
grep -cword1| grep -rword2记录
它只是在第一个管道命令之后进行填充。
为什么?
-c
?这只会显示比赛的数量。同样,没有理由使用 -r
。我建议你阅读 man grep
。 要对同一行上存在的2个单词进行grep,只需执行:
grepword1FILE | grepword2
grepword1FILE
将从FILE中打印出其中有word1的所有行,然后 grepword2
将打印其中包含word2的行。因此,如果您使用管道将它们组合在一起,它将显示包含word1和word2的行。
如果您只需要计算2个单词上有多少行同样的行,做:
grepword1FILE | grep -cword2
另外,为了解决您的问题,为什么它会卡住: grep -cword1
,你没有指定一个文件。因此, grep
需要来自 stdin
的输入,这就是它似乎挂起的原因。您可以按 Ctrl + D 发送EOF(文件结束),以便退出。
how do I grep for lines that contain two input words on the line? I'm looking for lines that contain both words, how do I do that? I tried pipe like this:
grep -c "word1" |grep -r "word2" logs
it just stucks after the first pipe command. why?
Why do you pass -c
? That will just show the number of matches. Similarly, there is no reason to use -r
. I suggest you read man grep
.
To grep for 2 words existing on the same line, simply do:
grep "word1" FILE | grep "word2"
grep "word1" FILE
will print all lines that have word1 in them from FILE, and then grep "word2"
will print the lines that have word2 in them. Hence, if you combine these using a pipe, it will show lines containing both word1 and word2.
If you just want a count of how many lines had the 2 words on the same line, do:
grep "word1" FILE | grep -c "word2"
Also, to address your question why does it get stuck : in grep -c "word1"
, you did not specify a file. Therefore, grep
expects input from stdin
, which is why it seems to hang. You can press Ctrl+D to send an EOF (end-of-file) so that it quits.
这篇关于在同一行上存在2个字的grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!