2012-02-26 28 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

+0

二重包装を除外するには、ネガティブなルアラウンド[例えば、Perl](http://ideone.com/Gyfpl)を使用することができます。 – jfs

答えて

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

it [簡単な入力のために働く](http://ideone.com/WM7JM) – jfs

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 
関連する問題