0

I am searching for the definition of a macro in a project containing hundreds of source code files.

I am fairly sure the macros are all defined in a single file.

If I do a grep -r search for a single macro, over 1000 hits occur.

I would like to search each file, and find those files containing both macro names.

Can this be done with grep?

For example something like this:

grep -r "MACRO1" AND "MACRO2" ./ 
0

3 Answers 3

1

Another way is

grep -rlZ 'MACRO1' ./ | xargs -0 grep -l 'MACRO2' 
  • grep -rlZ 'MACRO1' ./ will give filenames containing MACRO1
    • -Z is to separate filenames with null character
  • xargs -0 grep -l 'MACRO2' will give filenames containing MACRO2
Sign up to request clarification or add additional context in comments.

Comments

1

Use:

grep -Pzrl "(?s)(MACRO1.+MACRO2\|MACRO2.+MACRO1)" ./ 
  • -P perl regex
  • -z changes newline to null character.
  • -r recursive
  • -l print only filename

  • (?s) makes . matching newlines

  • MACRO1.+MACRO2 find when MACRO1 is before MACRO2
  • MACRO2.+MACRO1 find when MACRO2 is before MACRO1

1 Comment

nitpick: -z uses null character as separator instead of newline character... so if input file contains null character, it won't read whole file all at once..
0

with gnu grep:

grep -lrZ 'macro1' * |xargs -0 grep -l 'macro2' 

it will print files names that contains both macros.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.