grep 可以只显示匹配搜索模式的单词吗? [英] Can grep show only words that match search pattern?

查看:10
本文介绍了grep 可以只显示匹配搜索模式的单词吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让 grep 从匹配搜索表达式的文件中输出单词"?

如果我想在多个文件中找到th"的所有实例,我可以这样做:

grep "th" *

但输出将类似于(粗体是我的);

<前>some-text-file : the 猫坐在 the 垫子上some-other-text-file : the 快速的棕色狐狸另一个文本文件:我希望 this 解释它彻底

使用相同的搜索,我希望它输出的是:

这这这彻底

这可以使用 grep 吗?还是使用其他工具组合?

解决方案

试试 grep -o:

grep -oh "w*thw*";*

匹配菲尔的评论.

来自文档:

-h, --no-filename禁止输出文件名的前缀.这是默认的当只有一个文件(或只有标准输入)要搜索时.-o, --only-matching只打印匹配行的匹配(非空)部分,每个这样的部分都在一个单独的输出线上.

Is there a way to make grep output "words" from files that match the search expression?

If I want to find all the instances of, say, "th" in a number of files, I can do:

grep "th" *

but the output will be something like (bold is by me);

some-text-file : the cat sat on the mat  
some-other-text-file : the quick brown fox  
yet-another-text-file : i hope this explains it thoroughly 

What I want it to output, using the same search, is:

the
the
the
this
thoroughly

Is this possible using grep? Or using another combination of tools?

解决方案

Try grep -o:

grep -oh "w*thw*" *

Edit: matching from Phil's comment.

From the docs:

-h, --no-filename
    Suppress the prefixing of file names on output. This is the default
    when there is only  one  file  (or only standard input) to search.
-o, --only-matching
    Print  only  the matched (non-empty) parts of a matching line,
    with each such part on a separate output line.

这篇关于grep 可以只显示匹配搜索模式的单词吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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