2012-02-26 78 views
1

我正在寻找最佳方法来查找文件中的特定文本,并从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(已经发生,即包装),那么它不应该被重复。

+2

看看SED与'锚启动\ nwrapping-start'和''用包装锚end'取代'锚start' -end \ nanchor-end' – scibuff 2012-02-26 20:17:54

+0

排除双重包装,你可以使用负面的环视[例如,在Perl](http://ideone.com/Gyfpl)。 – jfs 2012-02-26 22:00:12

回答

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 
+0

它[适用于简单输入](http://ideone.com/WM7JM) – jfs 2012-02-26 22:02:51

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 
相关问题