0

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

basil
  • 690
  • 2
  • 11
  • 30
  • 2
    What regex flavor is this? Does it support lookaheads/lookbehinds? Please tag your programming language. – 4castle Sep 08 '16 at 03:01
  • 1
    Possible duplicate of [Regular expression to match line that doesn't contain a word?](http://stackoverflow.com/questions/406230/regular-expression-to-match-line-that-doesnt-contain-a-word) – phuclv Sep 08 '16 at 03:05
  • `<(?!XXX>)[^>]*>` – 4castle Sep 08 '16 at 03:05
  • **You can refer to it** http://stackoverflow.com/questions/406230/regular-expression-to-match-line-that-doesnt-contain-a-word?noredirect=1&lq=1 – YaHui Jiang Sep 08 '16 at 03:15
  • 4castle's answer works when I try it on regexr.com, but doesn't seem to work when I run it on my machine. – basil Sep 08 '16 at 03:51
  • @basil Read the duplicate question. Look at `grep` options; consider if `-E` is what you want. – Phrogz Sep 08 '16 at 03:51
  • I read the question posted. I feel it does not apply here, hence why I asked the question. I have tried egrep as well, to no avail. – basil Sep 08 '16 at 03:55
  • 1
    use `-P` option..`grep -P <(?!XXX>)[^>]*>` – rock321987 Sep 08 '16 at 04:31
  • rock321987's solution works. -P is perl, right? I should be able to pipe this through perl right? I tried: **cat temp.txt | perl -pne 'if ($1 =~ /<(?!(XXX)>)[^>]*>/){ print $1;}'** but that didn't seem to work. – basil Sep 08 '16 at 15:30

1 Answers1

1

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>/'
kdhp
  • 2,096
  • 14
  • 15