我正在尝试将以下文件中的“”之间的任何内容替换为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"
谢谢。
答案 0 :(得分:1)
第一个示例:
$ sed -e 's/\".*\"/xx/g' eem.txt
双引号不会出现在替换项中,因为您从未将双引号放在此处。如果要替换为"xx"
,请执行以下操作:
$ sed -e 's/\".*\"/"xx"/g' eem.txt
第二个例子:
$ sed -n 's/"[^"]*"/"xx"/gp' eem.txt
这将替换为"xx"
,但这仅是因为您在替换中使用了双引号。对于"[^"]*"
,这只是说要匹配双引号,后跟任意数量的 non 双引号字符,然后是另一个双引号。
在某些正则表达式中,例如Perl,我们也可以编写".*?"
来做同样的事情。这称为惰性点,表示在发现 first 双引号之前它将消耗所有东西。