-1

I have this text:

        NBA:red this line has a tab and ends with a curly braces}
    some random text qwertyuiop
    NBA:green this line must match
            NBA:red this line has a tab and must match
      NBA:response this line has spaces and must match
            NBA:blue this line has a tab and ends with a curly braces}
    some random text qwertyuiop
      NBA:blue this line has spaces at the begining and ends with curly braces}
    random text qwertyuiop
    this line must not match}
            this line must not match }

I want to match the lines that contains 'NBA:' following by the word 'red' or 'green' or 'blue', and also that doesn't end with a curly braces'}', this command match only 'NBA:' and one of the three words:

$ egrep 'NBA:(red|green|blue)' myfile.txt
        NBA:red this line has a tab and ends with a curly braces}
    NBA:green this line must match
            NBA:red this line has a tab and must match
            NBA:blue this line has a tab and ends with a curly braces}
      NBA:blue this line has spaces at the begining and ends with curly braces}

But I don't know how to match the lines that doesn't end with '}': I tried this but it doesn't work:

egrep 'NBA:(red|green|blue)*[^}]$' myfile.txt

But this works:

egrep 'NBA:(red|green|blue)' lorem.txt | egrep '[^}]$'
    NBA:green this line must match
            NBA:red this line has a tab and must match

I want to do it in just one command

noe vaz
  • 29
  • 4
  • 1
    Re "I want to do it in just one command", I often amused by this attitude. Are you getting charged money per command you execute? :-) The beauty of UNIX pipelines is the fact that you *can* string together many filters into a pipeline to achieve what you need. – paxdiablo Jan 19 '20 at 02:31
  • I just want to learn how to do it :-), I have no problems with the number of commands and pipes – noe vaz Jan 19 '20 at 02:32

1 Answers1

0

You were just one character off. This should work fine:

egrep 'NBA:(red|green|blue).*[^}]$'
#                          ^
#                   Note this bit.

* doesn't mean the same thing in regex that it does in glob patterns. It means zero-or-more of the preceding item (a preceding item in this answer being ., any character).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
hobbs
  • 223,387
  • 19
  • 210
  • 288
  • thanks!!! I just start learning regex and didn't know that, I edited the question btw – noe vaz Jan 19 '20 at 02:38
  • Just found this, I will leave it here just in case someone else is struggling with this: In regular expressions, the asterisk is a metacharacter for zero or more instances of the preceding character. Without regular expressions, the asterisk is a wildcard, for zero or more instances of any character. – noe vaz Jan 19 '20 at 02:46