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

查看:14
本文介绍了如何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 个单词,只需执行以下操作:

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.

如果您只想计算同一行上有多少行,请执行以下操作:

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天全站免登陆