brucezepplin
brucezepplin

Reputation: 9752

grep lines in file that consist only of a given string

I am using the following

grep -rn "activity" * to return lines in files of a subdirectory. I get things like:

There was no activity
We reported no activity
There was some activity

I now want to limit this to lines where the only string is the word activity. All my searches seem to find limiting the output to the string itself, not the actual line that only consists of the string itself.

Any help would be great!

Upvotes: 0

Views: 62

Answers (2)

Aaron
Aaron

Reputation: 24802

grep has a --line-regex flag (shortened into -x) that does exactly what you need : when provided, the pattern activity will only match whole lines consisting of only "activity".

$ echo "There was no activity
We reported no activity
activity" | grep -x activity

activity

In your case, using grep -rxn activity * should do the trick.

Upvotes: 1

Michael Haar
Michael Haar

Reputation: 711

grep can be combined with a concept called regular expressions or re's for short. The following line should do the trick:

grep -rn "^activity$" *

^ - matches the beginning of the line.

$ - matches the end of the line.

Upvotes: 1

Related Questions