简体   繁体   English

sed 将两个搜索条件应用于单个行,然后替换

[英]sed apply two search conditions to a single line and then substitute

Id like to do do the following substitution我想做以下替换

sed 's/$/)/' python2.py

with the condition that a line contains print( but it may not contain print(""" .条件是一行包含print(但它可能不包含print("""

A line that it should not match would be应该匹配的行将是

print("""Some multi line text etc... 

While lines like this should be matched虽然应该匹配这样的行

print("Some single line text"

Now its easy to match one or to exclude the other but i fail to combine both conditions into one command.现在很容易匹配一个或排除另一个,但我无法将这两个条件组合到一个命令中。 For example例如

sed -ne '/print(/s/$/)/p' python2.py

Does match all lines with print( but also the ones which i would like to omit with print(""" . On the other hand i can skip all lines with print(""" with是否与print(匹配所有行,但也匹配我想用print("""省略的行。另一方面,我可以用print("""跳过所有行

sed -ne /print("""/! s/$/)/p python2.py

But that includes all other lines even those without print( . Is there any way to combine both conditions such that the substition is only applied when both conditions are true for a line?但这包括所有其他行,即使是那些没有print(的行。有没有办法将这两个条件结合起来,以便仅当两个条件对一行都为真时才应用替换?

As a note:作为说明:

I am aware that i can do two sed runs where i first do the substitutions for all print( lines and then do a second sed run where i remove the newly substituted ) only in the print(""" lines. I'd like to know if it is possible to do it in one go so that is not an answer that i am looking for.我知道我可以执行两次 sed 运行,我首先对所有print(行,然后执行第二次 sed 运行,我删除新替换的)仅在print("""行。我想知道是否可以在一个 go 中做到这一点,所以这不是我正在寻找的答案。

You may use this sed that ignore lines that already have a closing ) :您可以使用此sed忽略已关闭的行)

cat file
print("""Some multi line text etc...
print("Some single line text"
print("Some single ""line text"
print("Some single line text")

sed '/print("""/!s/print(".*[^)]$/&)/' file
print("""Some multi line text etc...
print("Some single line text")
print("Some single ""line text")
print("Some single line text")

Pattern print(".*[^)]$ matches a line with test from print(" with a non- ) character in the end.模式print(".*[^)]$匹配带有 test from print("最后带有非)字符的行。 &) places ) at the end of matching pattern. &))放在匹配模式的末尾。

Is this all you're trying to do?这就是你想要做的一切吗?

$ cat file
print("""Some multi line text etc...
print("Some single line text"

$ sed 's/print("[^"].*/&)/' file
print("""Some multi line text etc...
print("Some single line text")

If not then please edit your question to show more truly representative sample input/output that includes cases this doesn't work for.如果不是,请编辑您的问题以显示更真正具有代表性的示例输入/输出,其中包括不适用的案例。

With GNU sed, you can use使用 GNU sed,您可以使用

sed '/print("""/!{/print("/s/$/)/}' file > newfile

See the online demo .请参阅在线演示

Details :详情

  • /print("""/! - if print(""" is found, skip the line /print("""/! - 如果找到print(""" ,则跳过该行
  • {/print("/s/$/)/} - else, if line contains print(" , replace the end of line with ) . {/print("/s/$/)/} - 否则,如果行包含print(" ,则将行尾替换为)

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

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