如何 grep 查找同一行中存在的两个单词? [复制]

2024-04-22

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

grep -c "word1" | grep -r "word2" logs

它只是在第一个管道命令之后卡住了。

Why?


为什么你会通过-c?这只会显示匹配的数量。同样,没有理由使用-r。我建议你阅读man grep.

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

grep "word1" FILE | grep "word2"

grep "word1" FILE将从 FILE 中打印所有包含 word1 的行,然后grep "word2"将打印其中包含 word2 的行。因此,如果使用管道将它们组合起来,它将显示同时包含 word1 和 word2 的行。

如果您只想计算这两个单词在同一行上的行数,请执行以下操作:

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.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何 grep 查找同一行中存在的两个单词? [复制] 的相关文章

随机推荐