我试图将以下文件中的“”替换为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"谢谢。
发布于 2019-05-15 04:53:20
第一个例子是:
$ sed -e 's/\".*\"/xx/g' eem.txt双引号不会出现在替换项中,因为您从未将它们放在那里。如果要用"xx"替换,请执行以下操作:
$ sed -e 's/\".*\"/"xx"/g' eem.txt第二个例子是:
$ sed -n 's/"[^"]*"/"xx"/gp' eem.txt这将替换为"xx",但只因为您在替换中使用了双引号。至于"[^"]*",这只是说要匹配一个双引号,后面跟着任意数量的非双引号字符,然后是另一个双引号。
在某些正则表达式中,例如Perl,我们也可以编写".*?"来做同样的事情。这就是所谓的懒惰点,这意味着它将消耗任何东西,直到找到第一个双引号。
https://stackoverflow.com/questions/56141916
复制相似问题