Reputation: 444
I am trying to grep a file for the words that contain "owner" but not the word owner itself. So, "ownership" for example would be counted.
I know:
grep -o -c owner #Print only matched words. ship(owner)ship --> owner
grep -w -c owner #Match only whole words. ownership (No), owner (Yes)
But it returns the whole standalone word "owner" still.
Whats the correct way to do this?
Upvotes: 0
Views: 159
Reputation: 67567
try this
grep -Pc '(\wowner)|(owner\w)' file
the word should have either a prefix or suffix (so the standalone won't match). Note that this will count the number of lines that have a match. To count the occurances
grep -oP '(\wowner)|(owner\w)' file | wc -l
Upvotes: 1