简体   繁体   English

sed析取匹配顺序

[英]sed order of disjunction matches

Given the input file text.txt 给定输入文件text.txt

First token second pattern, second pattern.

And running: 并运行:

sed -r -i "s/pattern|second pattern/REPLACE/g" input.txt

I get the new input.txt: 我得到了新的input.txt:

First token REPLACE, REPLACE.

It looks like second pattern is applied first here to match the contents of input.txt. 似乎在这里首先应用了second pattern以匹配input.txt的内容。

What is the order in which the disjuctions in sed are matching with the input text in sed? sed中的歧义与sed中的输入文本匹配的顺序是什么?

Is there a way to specify the order in which disjunctions of matches should be applied, that works together with the -i inline command? 有没有一种方法可以指定应用分离的顺序(与-i inline命令一起使用)?

Not sure exactly what the sed regex engine is doing with the alternation ( | ) operator, but one way to get your desired result might be to separate into two regex statements in sed: 不确定sed正则表达式引擎使用交替( | )运算符到底是做什么的,但是要获得所需结果的一种方法可能是将sed分为两个regex语句:

sed -r -i "s/pattern/REPLACE/g;s/second pattern/REPLACE/g"

This should give you total unambiguous control over the order in which the substitutions are applied, while all still happening in one sed invocation that works well with the inplace editing ( -i ). 这将使您对应用替换的顺序完全清楚地控制,而所有操作仍在一次sed调用中发生,并且与就地编辑( -i )一起很好地起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM