Johnathan
Johnathan

Reputation: 877

awk regexp when not match

I got some help before here to construct a regexp that would match any lines that started with ":" then anything, then a "(" and after that anything, but it could not end with a ")"

The regexp I used was this:

/:.+ \(.[^)]*$/

It seemes to work reasonably well. I had this input lines:

:web_application_settings (
:isakmp.phase1_DH_groups (
:ike_p1 (
:ike_p1_dh_grp (ReferenceObject
:isakmpkeymanager (lalalal)

And the output was only: :ike_p1_dh_grp (ReferenceObject

But then I happened to add the line:

:isakmpkeymanager ()

This shouldnt match, but it does, which I think is strange. What did I miss in the regexp that allowed this line that ends with a ")" to still match, when I use Do not match: [^)] followed by start to allow it to not match and then end of line.

Upvotes: 1

Views: 272

Answers (2)

tripleee
tripleee

Reputation: 189648

The . after the \( allows any single arbitrary character - including ) - immediately after the opening parenthesis. If that's not what you want to say, take it out.

Upvotes: 1

John1024
John1024

Reputation: 113924

Try:

awk '/:.+ \([^)]+$/' File

What went wrong

Let's look at the original regex, /:.+ \(.[^)]*$/, one step at a time:

  1. : meaning a single colon

  2. .+ meaning one or more of any character. (+ means one or more.)

  3. meaning one blank

  4. \( meaning one open parens

  5. . meaning one of any character

  6. [^)]* meaning zero or more of any character except close parens. (* means zero or more.)

  7. $ meaning the end of the line.

The problem was step 5 which can match a ). In the revised regex, this was eliminated and [^)]* was replaced with [^)]+.

Upvotes: 2

Related Questions