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
cat *-text-file | grep -Eio "th[a-z]+"
grep command for only matching and perl
grep -o -P 'th.*? ' filename
You can also try pcregrep. There is also a -w
option in grep, but in some cases it doesn't work as expected.
From Wikipedia:
cat fruitlist.txt
apple
apples
pineapple
apple-
apple-fruit
fruit-apple
grep -w apple fruitlist.txt
apple
apple-
apple-fruit
fruit-apple
It's more simple than you think. Try this:
egrep -wo 'th.[a-z]*' filename.txt #### (Case Sensitive)
egrep -iwo 'th.[a-z]*' filename.txt ### (Case Insensitive)
Where,
egrep: Grep will work with extended regular expression.
w : Matches only word/words instead of substring.
o : Display only matched pattern instead of whole line.
i : If u want to ignore case sensitivity.
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
Here are the example using ripgrep
:
rg -o "(\w+)?th(\w+)?"
It'll match all words matching th
.