2012-02-26 54 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 
相關問題