To exclude lines containing a specific word with grep, you can use the -v option. This option inverts the match, so it selects all lines that do not contain the specified word.
Syntax:
bash Copy code
grep -v “word” filename
Examples:
1. Exclude Lines Containing a Specific Word
If you want to exclude lines containing the word error from a file called log.txt:
bash Copy code
grep -v “error” log.txt
2. Exclude Lines Containing a Specific Word (Case Insensitive)
To exclude lines regardless of case, add the -i option:
bash Copy code
grep -vi “error” log.txt
3. Use with Multiple Words
To exclude lines containing multiple words, use -e for each word:
bash Copy code
grep -v -e “error” -e “warning” log.txt
4. Exclude Lines from Piped Input
You can also use grep -v with piped input. For example:
bash Copy code
cat log.txt | grep -v “error”
5. Exclude Lines and Save to a New File
To exclude lines and save the output to another file:
bash Copy code
grep -v “error” log.txt > filtered_log.txt
Explanation:
-v: Inverts the match, excluding lines with the specified word.
-i: Makes the search case insensitive.
-e: Allows specifying multiple patterns.