user
user

Reputation: 503

Sed regex to match content in between " "

I'm trying to replace anything between " " in the following file with xx.

ORIGINAL FILE

event syslog pattern "random text" maxrun 50 ratelimit 50
action 0.1 cli command "random text"
action 0.2 cli command "random text"
action 0.4 cli command "random text"
action 0.4 cli command "random text"
action 0.3 cli command "random text"
action 0.4 cli command "random text"
action 0.5 cli command "random text"
action 0.6 cli command "random text"
action 0.7 cli command "random text"
action 0.8 cli command "random text"
action 0.9 cli command "random text"
action 1.1 cli command "random text"
action 1.2 cli command "random text"
action 1.3 cli command "random text"
action 1.4 cli command "random text"
action 1.5 cli command "random text"
action 1.6 cli command "random text"
action 1.7 cli command "random text"
action 1.8 cli command "random text"
action 1.9 cli command "random text"
action 2.1 cli command "random text"
action 2.2 cli command "random text"
action 2.3 cli command "random text"

I've used sed for this, but the first case does not work. Can I get some explanation around the syntax?

Case 1: Why aren't the original " " retained when replacing with xx?

First attempt - non-working

$ sed -e 's/\".*\"/xx/g' eem.txt
event manager applet monitorHealth authorization bypass
event manager applet monitorHealth
event syslog pattern xx maxrun 50 ratelimit 50
action 0.1 cli command xx
action 0.2 cli command xx
action 0.4 cli command xx
action 0.4 cli command xx
action 0.4 cli command "undebug all”
action 0.3 cli command xx
action 0.4 cli command xx
action 0.5 cli command xx
action 0.6 cli command xx
action 0.7 cli command xx
action 0.8 cli command xx
action 0.9 cli command xx
action 1.1 cli command xx
action 1.2 cli command xx
action 1.3 cli command xx
action 1.4 cli command xx
action 1.5 cli command xx
action 1.6 cli command xx
action 1.7 cli command xx
action 1.8 cli command xx
action 1.9 cli command xx
action 2.1 cli command xx
action 2.2 cli command xx
action 2.3 cli command xx

Second attempt - working

Case 2: Can I get some explanation around "[^"]"? From what I have seen [^"] means do not match character ", but I'm unable to put the logic together.

$  sed -n 's/"[^"]*"/"xx"/gp' eem.txt
event syslog pattern "xx" maxrun 50 ratelimit 50
action 0.1 cli command "xx"
action 0.2 cli command "xx"
action 0.4 cli command "xx"
action 0.4 cli command "xx"
action 0.3 cli command "xx"
action 0.4 cli command "xx"
action 0.5 cli command "xx"
action 0.6 cli command "xx"
action 0.7 cli command "xx"
action 0.8 cli command "xx"
action 0.9 cli command "xx"
action 1.1 cli command "xx"
action 1.2 cli command "xx"
action 1.3 cli command "xx"
action 1.4 cli command "xx"
action 1.5 cli command "xx"
action 1.6 cli command "xx"
action 1.7 cli command "xx"
action 1.8 cli command "xx"
action 1.9 cli command "xx"
action 2.1 cli command "xx"
action 2.2 cli command "xx"
action 2.3 cli command "xx"

Thanks.

Upvotes: 1

Views: 43

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521629

For your first example:

$ sed -e 's/\".*\"/xx/g' eem.txt

The double quotes do not appear in the replacement, because you never put them there. If you want to replace with "xx", then do that:

$ sed -e 's/\".*\"/"xx"/g' eem.txt

For your second example:

$  sed -n 's/"[^"]*"/"xx"/gp' eem.txt

This will replace with "xx", but only because you used double quotes in the replacement. As for "[^"]*", this just says to match a double quote, followed by any number of non double quote characters, followed by another double quote.

In some regex flavors, e.g. Perl, we could also write ".*?" to do the same thing. This is called the lazy dot, and means that it will consume anything up until the first double quotation found.

Upvotes: 1

Related Questions