How to make grep command return entire matching line
How can I make the command grep -w
show the entire line that contains the match?
I need to force pattern to match whole words, but I need to see all of the line.
Here is my command:
cat /var/log/message.log | grep -w foobar
Solution 1:
If I am not mistaken, grep
shows the whole line for which a match has been found.
For a specific word, I use grep
directly instead of cat | grep
.
grep -w "foobar" /var/log/messages.log
If you do not see any other output, it would mean that there isn't anything else on that line.
Solution 2:
An alternative approach (beside -w option) is to do matching whole lines by an appropriate pattern:
cat /var/log/message.log | grep -P \^\.\*foobar\.\*\$
or case insensitive:
cat /var/log/message.log | grep -iP \^\.\*foobar\.\*\$
One advantage here is that it could be used file indepndent, like:
xauth list | grep -iP \^\.*foobar\.\*\$