gpt4 book ai didi

linux - sed 在多行的两个标签之间查找和替换

转载 作者:IT王子 更新时间:2023-10-29 01:09:08 25 4
gpt4 key购买 nike

我想找到并替换一个模式,其中

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
"

附言Stackoverflow 上的这些问题没有帮助。 sed : printing lines between two words only when one of the line matches a third word or any pattern

Regex with sed, search across multiple lines

最佳答案

使用 sed 你可以尝试这样的事情:

sed -e ':a;N;$!ba' -e 's#<file.*</file>#<sometext>\nvalue1\n</sometext>#' file

我的 sed 有点生疏,但我们在这里做的是使用 :a;N;$!ba 我们有效地在模式空间中创建了一个长行,以便我们可以应用第二个表达式来代替你。

这可能需要 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

关于linux - sed 在多行的两个标签之间查找和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22207829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com