首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sed正则表达式与“”之间的内容匹配

Sed正则表达式与“”之间的内容匹配
EN

Stack Overflow用户
提问于 2019-05-15 04:46:07
回答 1查看 38关注 0票数 1

我试图将以下文件中的“”替换为xx。

原始文件

代码语言:javascript
复制
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替换时不保留原始的“”?

第一次尝试-不工作

代码语言:javascript
复制
$ 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:我能得到一些关于“^”的解释吗?从我所看到的^“意思是不匹配字符",但我不能把逻辑放在一起。

代码语言:javascript
复制
$  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"

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-15 04:53:20

第一个例子是:

代码语言:javascript
复制
$ sed -e 's/\".*\"/xx/g' eem.txt

双引号不会出现在替换项中,因为您从未将它们放在那里。如果要用"xx"替换,请执行以下操作:

代码语言:javascript
复制
$ sed -e 's/\".*\"/"xx"/g' eem.txt

第二个例子是:

代码语言:javascript
复制
$  sed -n 's/"[^"]*"/"xx"/gp' eem.txt

这将替换为"xx",但只因为您在替换中使用了双引号。至于"[^"]*",这只是说要匹配一个双引号,后面跟着任意数量的非双引号字符,然后是另一个双引号。

在某些正则表达式中,例如Perl,我们也可以编写".*?"来做同样的事情。这就是所谓的懒惰点,这意味着它将消耗任何东西,直到找到第一个双引号。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56141916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档