【发布时间】:2012-02-26 20:15:41
【问题描述】:
我正在寻找从 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(即包装已经发生),那么它不应该被复制。
【问题讨论】:
-
查看 sed 并将
anchor-start替换为anchor-start\nwrapping-start和anchor-end替换为wrapping-end\nanchor-end -
要排除双重包装,您可以使用否定环视e.g., in Perl。