Regex 在文件中查找特定文本并与其他文本一起换行
我正在寻找最好的方法来查找文件中的特定文本,并从Linux命令行将其他文本环绕在其周围 因此,例如,该文件可能包含以下内容:Regex 在文件中查找特定文本并与其他文本一起换行,regex,linux,sed,awk,Regex,Linux,Sed,Awk,我正在寻找最好的方法来查找文件中的特定文本,并从Linux命令行将其他文本环绕在其周围 因此,例如,该文件可能包含以下内容: This is a test. anchor-start text to wrap will is in here anchor-end 因此,在上面的示例中,我希望找到介于锚定开始和锚定结束之间的文本,然后将该文本换行,以以下方式结束: This is a test. anchor-start wrapping-start text to wrap will is i
This
is
a
test.
anchor-start
text to wrap will is in here
anchor-end
因此,在上面的示例中,我希望找到介于锚定开始
和锚定结束
之间的文本,然后将该文本换行,以以下方式结束:
This
is
a
test.
anchor-start
wrapping-start
text to wrap will is in here
wrapping-end
anchor-end
我还应该注意到,我正在寻找一种解决方案,如果
锚定开始
之后紧接着包装开始
(即包装已经发生),则不应重复该解决方案。这可能适用于您:
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
查看sed并将
锚定起点
替换为锚定起点\n锚定起点
并将锚定终点
替换为锚定终点\n锚定终点
要排除双重环绕,可以使用负面环视。
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