如何grep表示同一行中存在的两个单词? [英] How to grep for two words existing on the same line?

查看:99
本文介绍了如何grep表示同一行中存在的两个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对行中包含两个输入单词的行进行grep?我正在寻找包含两个单词的行,我该怎么做?我试过这样的管道:

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.

为什么?

推荐答案

为什么要通过-c?那只会显示比赛的次数.同样,没有理由使用-r.我建议您阅读man grep.

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.

要对同一行中存在的2个单词进行grep,只需执行以下操作:

To grep for 2 words existing on the same line, simply do:

grep "word1" FILE | grep "word2"

grep "word1" FILE将打印FILE中所有包含word1的行,然后grep "word2"将打印其中包含word2的所有行.因此,如果使用管道将它们组合在一起,它将显示同时包含word1和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.

如果您只想统计同一行中有2个单词的行数,请执行以下操作:

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"

此外,要解决您的问题为什么卡住:在grep -c "word1"中,您未指定文件.因此,grep期望来自stdin的输入,这就是为什么它挂起的原因.您可以按 Ctrl + D 发送EOF(文件结束)以使其退出.

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.

这篇关于如何grep表示同一行中存在的两个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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