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

2 Answers

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.

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\.\*\$

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like