kurtgn
kurtgn

Reputation: 8732

grep: constructing a regex pattern to exclude several groups

I have a folder with three files:

$ ls

aaa.txt abc.txt def.txt

If I want to grep the output excluding the abc.txt file I can do:

$ ls | grep -v 'abc'

aaa.txt
def.txt

If I want to exclude two files I can do:

$ ls | grep -v 'abc' | grep -v 'def'

aaa.txt

But how can I do this using one regex and one grep invocation?

This does not work:

$ ls | grep -v '[(abc)(def)]'

neither does this:

$ ls | grep -v "abc|def"

Upvotes: 0

Views: 59

Answers (1)

Inian
Inian

Reputation: 85845

Use the ERE(Extended Regular Expression) pattern for the alternation match | which is not enabled by default in BRE (which grep uses by default)

grep -vE "abc|def"

or use the extended grep, i.e. egrep which enables the ERE by default

egrep -v "abc|def"

Upvotes: 1

Related Questions