Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

Having difficulty using regexp in grep

I'm probably missing something really obvious but I have an xml file that I need to grep to see if it contains this:

<phone>null</phone> or <phone></phone>

In any case, and with any additional spaces (between the tags).

So, my non working solution is this:

grep -i "\<phone\>\s*(null)?\s*\</phone\>" ./myfile.xml

But that returns empty (even though the file I'm using has this node). What am I overlooking?

TIA

Upvotes: 0

Views: 108

Answers (2)

Kent
Kent

Reputation: 195039

kent$  echo "<phone>null</phone> or <phone></phone>foo bar"|grep -iE "(<phone>null</phone>|<phone></phone>)"                             

<phone>null</phone> or <phone></phone>foo bar

Upvotes: 0

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

Ah! Found it... I'll answer it myself in case anyone else finds it useful in the future... adding the -P switch ie...

grep -i -P "\<phone\>\s*(null)?\s*\</phone\>" ./myfile.xml

...to the call makes it use perl style regex (which is what I'm using) and produces the desired result.

Upvotes: 1

Related Questions