HomeUnixGREP OR: Match Any Of Multiple Patterns

    GREP OR: Match Any Of Multiple Patterns

    Date:

    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……………………

    Book a 1-on-1
    Call Session

    Want Patrick's full attention? Nothing compares with a live one on one strategy call! You can express all your concerns and get the best and most straight forward learning experience.

    Related articles:

    UNIX GREP Command in Depth

    GREP Command GREP stands for Globally search a regular expression...

    Latest courses:

    Strategic Vision: Mastering Long-Term Planning for Business Success

    Introduction: Professional growth is a continuous journey of acquiring new...

    Leadership Excellence: Unlocking Your Leadership Potential for Business Mastery

    Introduction: Professional growth is a continuous journey of acquiring new...

    Marketing Mastery: Strategies for Effective Customer Engagement

    Introduction: Professional growth is a continuous journey of acquiring new...

    Financial Management: Mastering Numbers for Profitability and Sustainable Growth

    Introduction: Professional growth is a continuous journey of acquiring new...

    Innovation and Adaptability: Thriving in a Rapidly Changing Business Landscape

    Introduction: Professional growth is a continuous journey of acquiring new...