Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I know that grep -E 'termA|termB' files allows me to search multiple files for termA OR termB. What I would like to do instead is search for termA AND termB. They do not have to be on the same line as long as the two terms exists within the same file. Essentially a "search within result" feature.

I know I can pipe the results of one grep into another but that seems slow when going over many files.

grep -l "termA" * | xargs grep -l "termB" | xargs grep -E -H -n --color "termA|termB"

Hopefully the above isn't the only way to do this.

It would be extra nice if this could work on Windows (have cygwin) and Linux. I don't mind installing a tool to perform this task.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

You can use awk for this. Simply set a flag per match you're looking for, then check to see if all the flags have been set.

share|improve this answer
    
Can you give an example? –  nevets1219 May 28 '10 at 20:57
    
/regex1/ { flag1=1 } –  Ignacio Vazquez-Abrams May 28 '10 at 21:10
    
Thanks, I believe this will do what I want with some manipulation. –  nevets1219 May 28 '10 at 21:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.