How to exclude patterns?

0117

To exclude patterns when using grep, you can use the -v option. This option inverts the match, meaning that grep will display only the lines that do not match the specified pattern.

Basic Syntax

grep -v 'pattern' filename

Example

  1. Excluding a specific word:
    To display all lines from a file named data.txt that do not contain the word "error":

    grep -v 'error' data.txt
  2. Excluding multiple patterns:
    If you want to exclude lines containing either "error" or "warning", you can use grep with a regular expression:

    grep -v -E 'error|warning' data.txt
  3. Using with pipes:
    To exclude lines containing "completed" from the output of a command:

    ps aux | grep -v 'completed'

Using the -v option is an effective way to filter out unwanted lines from your search results.

0 Comments

no data
Be the first to share your comment!