Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .