SED - 替换占位符之间的文本

Cap*_*ack 1 unix linux bash terminal sed

我需要你们中的一些 SED 向导来帮助一个菜鸟....

我正在使用 SED 替换某些占位符之间的文本。问题是它们在不同的行上(显然 SED 讨厌这一点)。

我需要替换的文本在“#SO”和“#EO”之间,如下所示:

#SO
I need to replace this text
#EO
Run Code Online (Sandbox Code Playgroud)

我想出了这个:

sed -ni '1h; 1!H; ${ g; s/#SO\(.*\)#EO Test/1/REPLACEMENT/ p }' foo.txt
Run Code Online (Sandbox Code Playgroud)

我刚刚开始掌握 SED,所以我可能完全错了,但任何建议都会很棒。

dog*_*ane 6

使用sed如下图:

$ cat file
line 1
line 2
#SO
I need to replace this text
#EO
line 3

$  sed -n '/#SO/{p;:a;N;/#EO/!ba;s/.*\n/REPLACEMENT\n/};p' file
line 1
line 2
#SO
REPLACEMENT
#EO
line 3
Run Code Online (Sandbox Code Playgroud)

这个怎么运作:

/#SO/{                       # when "#SO" is found
  p                          # print
  :a                         # create a label "a"
    N                        # store the next line
  /#EO/!ba                   # goto "a" and keep looping and storing lines until "#EO" is found
  s/.*\n/REPLACEMENT\n/      # perform the replacement on the stored lines
}
p                            # print
Run Code Online (Sandbox Code Playgroud)