Quick Earn Money

How do I match all lines not matching a particular pattern using grep? I tried this: grep '[^foo]'

 

The grep command with a pattern like [foo] will match any line that contains any character other than 'f', 'o', or 'o'. This is not the correct way to match all lines that do not contain a specific pattern.

To match all lines that do not match a particular pattern using grep, you can use the -v option, which inverts the matching. Here's how you can do it:

bash
grep -v 'pattern' filename

Replace 'pattern' with the pattern you want to exclude and filename with the name of the file you want to search in.

For example, if you have a file named data.txt and you want to match all lines that do not contain the word "apple", you can use the following command:

bash
grep -v 'apple' data.txt

This will show all lines in data.txt that do not contain the word "apple".

Comments