GREP OR: Match Any Of Multiple Patterns

GREP OR: Match Any Of Multiple Patterns-Find all the lines of a file, that match any of provided patterns.

We can use grep and egrep commands:

Syntex:

grep "PATTERN1\|PATTERN2" FILE
grep -E "PATTERN1|PATTERN2" FILE
grep -e PATTERN1 -e PATTERN2 FILE
egrep "PATTERN1|PATTERN2" FILE

Lets see it with the help of examples one by one:

I will use a log file to demonstrate above command:

abc.log is having pattern as below:

———————————————————————————————–suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………
khozbikar………………………33333……………………


1. Grep command with single search pattern:

Command:  grep 1111 abc.log

output: techsarthi……………..1111………………………(it will print the line having search pattern)

2.Grep command with multiple search pattern:

Command: grep “1111\|2222” abc.log

output: (above command will print all the lines having pattern 1111 or 2222)

suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………

3. Grep command with multiple search pattern:

Command:  grep -E “1111|2222” abc.log

output:  (above command will print all the lines having pattern 1111 or 2222)

suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………

4.Grep command with multiple search pattern:

Command:  grep -e 1111 -e 2222 abc.log

output:  (above command will print all the lines having pattern 1111 or 2222)

suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………

5. egrep command with multiple search pattern:

Command: egrep “1111|2222” abc.log

output:  (above command will print all the lines having pattern 1111 or 2222)

suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………

6.Grep command with multiple search pattern:

Command:  grep -E “1111|2222|3333” abc.log

output:  (above command will print all the lines having pattern 1111 or 2222 or 3333)

suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………
khozbikar………………………33333……………………

7 Replies to “GREP OR: Match Any Of Multiple Patterns”

  1. I am curious to find out what blog platform you are working
    with? I’m experiencing some minor security problems with
    my latest website and I’d like to find something more safeguarded.
    Do you have any suggestions?

  2. I don’t even know how I stopped up here, however I assumed this publish was great.

    I do not realize who you are however certainly you are
    going to a famous blogger if you happen to are not already.
    Cheers!

  3. A person essentially assist to make critically articles I’d state.
    That is the very first time I frequented your web page and so far?

    I surprised with the analysis you made to make this particular post extraordinary.

    Wonderful activity!

  4. Fascinating blog! Is your theme custom made or did you
    download it from somewhere? A theme like yours with a few simple adjustements would
    really make my blog stand out. Please let me know where you got your design. Kudos

  5. hello there and thank you for your information – I have definitely picked up something new from right here.

    I did however expertise several technical issues using this web site, as I experienced
    to reload the website many times previous to
    I could get it to load properly. I had been wondering if your web host is OK?

    Not that I am complaining, but slow loading instances times will
    often affect your placement in google and can damage your high-quality score if ads and
    marketing with Adwords. Anyway I’m adding this RSS to my email
    and could look out for much more of your respective intriguing content.
    Ensure that you update this again very soon.

  6. Thanks for the good writeup. It if truth be told was a entertainment account it.
    Look complex to far brought agreeable from you! However, how could we keep in touch?

Leave a Reply

Your email address will not be published. Required fields are marked *