简体   繁体   中英

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!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM