如何grep存在于同一行上的两个单词?

编程入门 行业动态 更新时间:2024-10-09 05:13:29
本文介绍了如何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存在于同一行上的两个单词?

本文发布于:2023-10-31 10:18:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545788.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单词   两个   grep

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!