Alex Rivera | Logout

How to grep for two words existing on the same line?

Asked 2011-06-25T21:36:10.267
179

How do I grep for lines that contain two input words on the line? I'm looking for lines that contain both words, how do I do that? I tried pipe like this:

grep -c "word1" | grep -r "word2" logs

It just stucks after the first pipe command.

Why?

Edit
Report

1 Answer

7

The main issue is that you haven't supplied the first grep with any input. You will need to reorder your command something like

grep "word1" logs | grep "word2"

If you want to count the occurences, then put a '-c' on the second grep.

answered 2012-11-26T09:54:01.307

Your Answer