gpt4 book ai didi

regex - 在文件中查找特定文本并用其他文本包裹

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:27:25 25 4
gpt4 key购买 nike

我正在寻找在文件中查找特定文本并将其他文本从 Linux 命令行包裹起来的最佳方法。

例如,文件可能包含以下内容:

This
is
a
test.
anchor-start
text to wrap will is in here
anchor-end

所以在上面的示例中,我想找到 anchor-startanchor-end 之间的文本,然后包装该文本,以便我最终得到:

This
is
a
test.
anchor-start
wrapping-start
text to wrap will is in here
wrapping-end
anchor-end

我还应该注意,我正在寻找一种解决方案,如果 anchor-start 紧随其后是 wrapping-start(即包装已经发生),那么它不应重复。

最佳答案

这可能对你有用:

sed '/anchor-start/,/anchor-end/{/anchor-start/{h;d};H;/anchor-end/{x;/wrapping-start/b;s/\n.*\n/\nwrapping-start&wrapping-end\n/p};d}' file
This
is
a
test.
anchor-start
wrapping-start
text to wrap will is in here
wrapping-end
anchor-end

关于regex - 在文件中查找特定文本并用其他文本包裹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9456756/

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