Can grep show only words that match search pattern

2019-01-01 01:30发布

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?

标签: grep words
14条回答
千与千寻千般痛.
2楼-- · 2019-01-01 02:09

I had a similar problem, looking for grep/pattern regex and the "matched pattern found" as output.

At the end I used egrep (same regex on grep -e or -G didn't give me the same result of egrep) with the option -o

so, I think that could be something similar to (I'm NOT a regex Master) :

egrep -o "the*|this{1}|thoroughly{1}" filename
查看更多
谁念西风独自凉
3楼-- · 2019-01-01 02:14

grep command for only matching and perl

grep -o -P 'th.*? ' filename
查看更多
登录 后发表回答