Sed regex匹配中间的内容&引用;

Sed regex匹配中间的内容&引用;,sed,Sed,我正在尝试将以下文件中“”之间的任何内容替换为xx 原始文件 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 cl

我正在尝试将以下文件中“”之间的任何内容替换为xx

原始文件

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"
我已经为此使用了sed,但是第一个案例不起作用。我能解释一下语法吗

案例1:为什么用xx替换时不保留原来的“”

第一次尝试-不工作

$ 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
第二次尝试-工作

案例2:我可以得到一些关于“[^”]”的解释吗?从我所看到的情况来看,[^”]表示字符不匹配,但我无法把逻辑放在一起

$  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"

谢谢。

关于您的第一个示例:

$ sed -e 's/\".*\"/xx/g' eem.txt
双引号不会出现在替换中,因为您从未将它们放在那里。如果要替换为
“xx”
,请执行以下操作:

$ sed -e 's/\".*\"/"xx"/g' eem.txt
第二个例子是:

$  sed -n 's/"[^"]*"/"xx"/gp' eem.txt
这将替换为
“xx”
,但这仅仅是因为您在替换中使用了双引号。至于
“[^”]*”
,这只是说匹配一个双引号,后跟任意数量的非双引号字符,后跟另一个双引号


在一些正则表达式风格中,例如Perl,我们也可以编写
“*?”
来做同样的事情。这称为惰性点,意味着在找到第一个双引号之前,它将消耗任何东西。

这是否意味着在本例中[^“]*”在功能上等同于\“*”?由于示例文件中只有一个双引号实例。不,它们不相同,至少在
*
可以跨行匹配的情况下不一样。通常,
“*”
将匹配双引号,直到最后一个双引号。如果您想在双引号中匹配每个术语,那么您应该使用
“[^”]*”
“*?”
,具体取决于您的正则表达式风格。“undebug all”包含一个与ASCII双引号字符不匹配的结束双引号(
UTF-8中的E2 80 9D
)。