15

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)

4 Answers 4

24

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
Sign up to request clarification or add additional context in comments.

Comments

12

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.

Comments

5

I use

grep -E 'foo|bar' filename.txt

Comments

0

I managed it with the following command:

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

2 Comments

Um, isn't this the answer I gave you? I would be happy if you could at least upvote my answer if it helped you. Answering (your own question) with an answer from someone other is kinda rude.
Sorry i have miss out, i'm out of office over the weekends

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.