我正在寻找找到文件中特定文本的最佳方法,并从Linux命令行中包装其他文本。
例如,该文件可能包含以下内容:
This
is
a
test.
anchor-start
text to wrap will is in here
anchor-end
因此,在上面的示例中,我想在anchor-start
和anchor-end
之间找到文本,然后将该文本换行以便最终得到:
This
is
a
test.
anchor-start
wrapping-start
text to wrap will is in here
wrapping-end
anchor-end
我还应该注意到,我正在寻找一种解决方案,如果anchor-start
后面紧跟wrapping-start
(即包裹已经发生),则不应该重复。
答案 0 :(得分:2)
这可能对您有用:
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
答案 1 :(得分:0)
pearl.319> cat temp.pl
This
is a
test.
anchor-start
text to wrap will
is in here
anchor-end
pearl.320> perl -p -e 's/anchor-start/anchor-start\nwrap-start/g;s/anchor-end/wrap-end\nanchor-end/g' temp.pl
This
is a
test.
anchor-start
wrap-start
text to wrap will
is in here
wrap-end
anchor-end