Skip to main content
3 of 4
Cleaned up. (Didn't change content.)
agc
  • 7.4k
  • 4
  • 25
  • 54

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.)

Thushi
  • 9.7k
  • 4
  • 31
  • 46