basil
basil

Reputation: 720

exclude a string from search in regex

I want to be able to search for anything between brackets, but excluding a certain string. e.g. I do not want to search for XXX but anything else is okay.

<XXX> does not match but <XYZ> <YZ> etc do match

I am unsure how to use a wildcard to search for the string between the brackets but excluding XXX. How would I go about doing this?

edit: I'm just talking about basic grep

Upvotes: 0

Views: 77

Answers (1)

kdhp
kdhp

Reputation: 2116

Two expressions are necessary, one to match, one to exclude:

grep '<.*>' | grep -v '<XXX>'

If preferred, they can be put together into a single sed or awk script:

sed '/<.*>/!d;/<XXX>/d'

or

awk '/<.*>/&&!/<XXX>/'

Upvotes: 1

Related Questions