Tuesday, March 15, 2011

How can I grep for files that have both expressions?

Hello All,

I would like to display files that contain more than one expression and exclude files that do not have all expressions.

Any ideas?

Thanks!

From stackoverflow
  • egrep -r 'expression1|expression2|expression3' .

  • if you don't have the -r you can just use grep over again on the results grep expression1 * | grep expression 2 | grep expression 3

  • Usually I do that sort of thing by running grep multiple times, something like

    grep -l 'expression1' * | xargs grep -l 'expression2' | xargs grep -l 'expression3'
    

    and so on. It doesn't seem very efficient, and I wouldn't be surprised if there is a better way, but I don't know it.

    Vatine : With some domain knowledge (in this case "what regexp is least likely to match") and starting with that regexp, this is probably close to the most efficient solution.

0 comments:

Post a Comment