sed使用多行在两个标签之间查找并替换 [英] sed find and replace between two tags with multi line
本文介绍了sed使用多行在两个标签之间查找并替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要查找并替换其中的模式
I want to find and replace a pattern where
text="
hold1
hold2
<file option1='one'>
some text
some text
...
... more data
</file>
this1
that1
"
pattern="<file.*</file>"
replacewith="<sometext>
value1
</sometext>"
output text="
hold1
hold2
<sometext>
value1
</sometext>
this1
that1
"
P.S.这些关于Stackoverflow的问题无济于事. sed:仅当一行中的一个与第三个单词或任何模式匹配时才在两个单词之间打印行
P.S. These questions on Stackoverflow do not help. sed : printing lines between two words only when one of the line matches a third word or any pattern
推荐答案
使用sed
,您可以尝试以下操作:
Using sed
you can try something like:
sed -e ':a;N;$!ba' -e 's#<file.*</file>#<sometext>\nvalue1\n</sometext>#' file
我的sed
有点生锈,但是我们在这里使用的是:a;N;$!ba
,我们有效地在模式空间中创建了一条长行,以便我们可以应用第二个表达式来代替您.
My sed
is a little rusty but what we are doing here is using :a;N;$!ba
we effectively create one long line in pattern space so that we can apply the second expression which does your substitution.
这可能需要GNU sed
$ cat file
hold1
hold2
<file option1='one'>
some text
some text
more data
</file>
this1
that1
$ sed -e ':a;N;$!ba' -e 's#<file.*</file>#<sometext>\nvalue1\n</sometext>#' file
hold1
hold2
<sometext>
value1
</sometext>
this1
that1
这篇关于sed使用多行在两个标签之间查找并替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文