Ali Mohammadi
Ali Mohammadi

Reputation: 1324

grep -v except pattern

I want to grep -v file except pattern.

this is my file content (test.txt):

a
aaa
bbb
ccc

I want to this result:

aaa
bbb
ccc

And cat test.txt |grep -v "a" --exclude="aaa" is not correctly work and return this:

bbb
ccc

Upvotes: 0

Views: 328

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174706

You need to use word boundary \b which matches between a word character and a non-word character.

$ grep -v '\ba\b' file
aaa
bbb
ccc

OR

$ grep -v '^a$' file
aaa
bbb
ccc

^ Asserts that we are at the start of a line and $ asserts that we are at the end of a line.

Upvotes: 2

nu11p01n73R
nu11p01n73R

Reputation: 26667

$ grep -w -v "a" test.txt
aaa
bbb
ccc

From the man page

   -w, --word-regexp
          Select  only  those  lines  containing  matches  that form whole
          words.  

Upvotes: 1

Related Questions