Kenny Quah Kok Siong
Kenny Quah Kok Siong

Reputation: 79

Grep command with multiple patterns

Hi i am currently use this to grep:

$ grep -nri --exclude-dir=DELIVERY_REL "xxxxxx\.h" --colour --include=*.{c,h}

I am trying to fine tune the search results of my grep to include multiple patterns. I have tried multiple ways recommended on stack-overflow on similar questions, but to no avail. What i need to grep:

xxxxxx.h and #include or xxxxxx.h and # include (with a space)

Upvotes: 3

Views: 19722

Answers (4)

sana_space
sana_space

Reputation: 31

I use

grep -E 'foo|bar' filename.txt

Upvotes: 3

Kenny Quah Kok Siong
Kenny Quah Kok Siong

Reputation: 79

I managed it with the following command:

grep -nri --include=*.{c,h} --exclude-dir=DELIVERY_REL "xxxxxx\.h" | grep ":#include\|:# include" 

Upvotes: 0

Socowi
Socowi

Reputation: 27195

OR: To search for lines that match regex A or B specify each of the regexes with a preceding -e option in a single grep command: grep -e A -e B. Alternatively, use the alternation operator inside a single regex: grep -E 'A|B'

AND: To print only lines that match regex A and B chain two grep searches: grep A | grep B. Alternatively, list all possible orders in which the regex can match in a single regex: grep -E 'A.*B|B.*A'

For your specific case you could use

grep 'xxxxxx\.h' | grep -E '# ?include'

Here the ? after the space means that the space is optional.
# ?include is equivalent to #include|# include.

Upvotes: 7

Zig Razor
Zig Razor

Reputation: 3495

To include multiple pattern to grep command you need option "-e".

For example you can include pattern "pattern1" e "pattern2" in this way:

grep -e "pattern1" -e "pattern2" filename.ext

Upvotes: 6

Related Questions