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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Why do you pass
-c? That will just show the number of matches. Similarly, there is no reason to use-r. I suggest you readman grep.To grep for 2 words existing on the same line, simply do:
grep "word1" FILEwill print all lines that have word1 in them from FILE, and thengrep "word2"will print the lines that have word2 in them. Hence, if you combine these using a pipe, it will show lines containing both word1 and word2.If you just want a count of how many lines had the 2 words on the same line, do:
Also, to address your question why does it get stuck : in
grep -c "word1", you did not specify a file. Therefore,grepexpects input fromstdin, which is why it seems to hang. You can press Ctrl+D to send an EOF (end-of-file) so that it quits.