Skip to main content
\| isn't standard
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

There are lot of ways to use grep with logical operators.

  1. Use Using multiple \|-e to separate multipleoptions matches anything that matches any of the patterns for, giving the OR conditionoperation.

    Example:  grep 'pattern1\|pattern2'-e pattern1 -e pattern2 filename

  2. Use theIn extended regular expressions (grep -E option), you can use | to sendcombine multiple patterns forwith the OR condition operation.

    Example: grep -E 'pattern1|pattern2' filename

  3. Using a single -e matches only one pattern, but using multiple -e option matches more than one pattern.

    Example: grep -e pattern1 -e pattern2 filename

  4. grep -v can simulate the NOT operation.

  5. There is no AND operator in grep, but you can brute-force simulate AND by using themultiple patterns with -E| option.

    Example : grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename

    The above example will match all the lines that contain both pattern1 and pattern2 in either order.) This gets very ugly if there are more patterns to combine.

There are lot of ways to use grep with logical operators.

  1. Use \| to separate multiple patterns for the OR condition.

    Example:  grep 'pattern1\|pattern2' filename

  2. Use the -E option to send multiple patterns for the OR condition.

    Example: grep -E 'pattern1|pattern2' filename

  3. Using a single -e matches only one pattern, but using multiple -e option matches more than one pattern.

    Example: grep -e pattern1 -e pattern2 filename

  4. grep -v can simulate the NOT operation.

  5. There is no AND operator in grep, but you can brute-force simulate AND by using the -E option.

    Example : grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename

    The above example will match all the lines that contain both pattern1 and pattern2 in either order.)

There are lot of ways to use grep with logical operators.

  1. Using multiple -e options matches anything that matches any of the patterns, giving the OR operation.

    Example: grep -e pattern1 -e pattern2 filename

  2. In extended regular expressions (grep -E), you can use | to combine multiple patterns with the OR operation.

    Example: grep -E 'pattern1|pattern2' filename

  3. grep -v can simulate the NOT operation.

  4. There is no AND operator in grep, but you can brute-force simulate AND by using multiple patterns with |.

    Example : grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename

    The above example will match all the lines that contain both pattern1 and pattern2 in either order. This gets very ugly if there are more patterns to combine.

Cleaned up. (Didn't change content.)
Source Link
agc
  • 7.4k
  • 4
  • 25
  • 54

There are lot of ways to use grep with logical operators.

First Method

You can use the | with the escape functionality.You need to use \| to seperate multiple patterns for the or condition.

Example : grep 'pattern1\|pattern2' filename

Second method

you can use the -E option to send multiple patterns for the or condition.

Example : grep -E 'pattern1|pattern2' filename

Third Method

Using -e option you can send only one pattern but using multiple -e option you can send more than one pattern.

Example: grep -e pattern1 -e pattern2 filename

NOTE

There is no AND operator in grep but you can simulate AND using the -E option.

Example : grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename

The above example will grep all the lines that contain both pattern1 and pattern2. (Edit: Also in either order.)

UPDATE

Using grep -v you can simulate the NOT operation.

  1. Use \| to separate multiple patterns for the OR condition.

    Example: grep 'pattern1\|pattern2' filename

  2. Use the -E option to send multiple patterns for the OR condition.

    Example: grep -E 'pattern1|pattern2' filename

  3. Using a single -e matches only one pattern, but using multiple -e option matches more than one pattern.

    Example: grep -e pattern1 -e pattern2 filename

  4. grep -v can simulate the NOT operation.

  5. There is no AND operator in grep, but you can brute-force simulate AND by using the -E option.

    Example : grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename

    The above example will match all the lines that contain both pattern1 and pattern2 in either order.)

There are lot of ways to use grep with logical operators.

First Method

You can use the | with the escape functionality.You need to use \| to seperate multiple patterns for the or condition.

Example : grep 'pattern1\|pattern2' filename

Second method

you can use the -E option to send multiple patterns for the or condition.

Example : grep -E 'pattern1|pattern2' filename

Third Method

Using -e option you can send only one pattern but using multiple -e option you can send more than one pattern.

Example: grep -e pattern1 -e pattern2 filename

NOTE

There is no AND operator in grep but you can simulate AND using the -E option.

Example : grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename

The above example will grep all the lines that contain both pattern1 and pattern2. (Edit: Also in either order.)

UPDATE

Using grep -v you can simulate the NOT operation.

There are lot of ways to use grep with logical operators.

  1. Use \| to separate multiple patterns for the OR condition.

    Example: grep 'pattern1\|pattern2' filename

  2. Use the -E option to send multiple patterns for the OR condition.

    Example: grep -E 'pattern1|pattern2' filename

  3. Using a single -e matches only one pattern, but using multiple -e option matches more than one pattern.

    Example: grep -e pattern1 -e pattern2 filename

  4. grep -v can simulate the NOT operation.

  5. There is no AND operator in grep, but you can brute-force simulate AND by using the -E option.

    Example : grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename

    The above example will match all the lines that contain both pattern1 and pattern2 in either order.)

There are lot of ways to use grep with logical operators.

First Method

You can use the | with the escape functionality.You need to use \| to seperate multiple patterns for the or condition.

Example : grep 'pattern1\|pattern2' filename

Second method

you can use the -E option to send multiple patterns for the or condition.

Example : grep -E 'pattern1|pattern2' filename

Third Method

Using -e option you can send only one pattern but using multiple -e option you can send more than one pattern.

Example: grep -e pattern1 -e pattern2 filename

NOTE

There is no AND operator in grep but you can simulate AND using the -E option.

Example : grep -E 'pattern1.*pattern2'*pattern2|pattern2.*pattern1' filename

The above example will grep all the lines that contain both pattern1 and pattern2. (Edit: Also in either order.)

UPDATE

Using grep -v you can simulate the NOT operation.

There are lot of ways to use grep with logical operators.

First Method

You can use the | with the escape functionality.You need to use \| to seperate multiple patterns for the or condition.

Example : grep 'pattern1\|pattern2' filename

Second method

you can use the -E option to send multiple patterns for the or condition.

Example : grep -E 'pattern1|pattern2' filename

Third Method

Using -e option you can send only one pattern but using multiple -e option you can send more than one pattern.

Example: grep -e pattern1 -e pattern2 filename

NOTE

There is no AND operator in grep but you can simulate AND using the -E option.

Example : grep -E 'pattern1.*pattern2' filename

The above example will grep all the lines that contain both pattern1 and pattern2.

UPDATE

Using grep -v you can simulate the NOT operation.

There are lot of ways to use grep with logical operators.

First Method

You can use the | with the escape functionality.You need to use \| to seperate multiple patterns for the or condition.

Example : grep 'pattern1\|pattern2' filename

Second method

you can use the -E option to send multiple patterns for the or condition.

Example : grep -E 'pattern1|pattern2' filename

Third Method

Using -e option you can send only one pattern but using multiple -e option you can send more than one pattern.

Example: grep -e pattern1 -e pattern2 filename

NOTE

There is no AND operator in grep but you can simulate AND using the -E option.

Example : grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename

The above example will grep all the lines that contain both pattern1 and pattern2. (Edit: Also in either order.)

UPDATE

Using grep -v you can simulate the NOT operation.

Source Link
Thushi
  • 9.7k
  • 4
  • 31
  • 46
Loading