2011-07-19 14 views
0
私の設定の

一つは、特定のfooセクションがあります。SED/awkは:パターンにパターンから行を削除

# Configuration foo - Start 
blah 
blah 
blah 
# Configuration foo - End 

は、私は別のファイルの内容に置き換えることが、このセクションに変更を適用したいと思いますが(たとえば、 new_foo.txt)。セクションは常に# Configuration foo - Startで始まり、# Configuration foo - Startで終わります。

Ubuntu \ bashエコシステムでこれを達成する最も良い方法は何ですか?私は小さなPythonスクリプトを書くことができますが、私はそれがエレガントな1ライナーかもしれないと思う。あなたが1行にすべてを置く場合

答えて

4
sed -e '/^# Configuration foo - Start$/r new_foo.txt' -e '/^# Configuration foo - Start$/,/^# Configuration foo - End$/d' 
+0

非常に印象的! –

3
{ 
    sed -n '0,/^# Configuration foo - Start$/p' infile 
    cat new_foo.txt 
    sed -n '/^# Configuration foo - End$/,$p' infile 
} > outfile 

はその後;}の前にスペースを入れることを忘れないでください。

1

new_foo.txtと仮定すると、メモリ内に保持されるのに十分小さいですが、これは私の作品:

awk 'NR==FNR {A[i++] = $0} NR>FNR && !exclude {print $0} /# Configuration foo - Start/ {exclude=1; for (line in A) { print line }} /# Configuration foo - End/ {exclude=0; print$0}' new_foo.txt config 
関連する問題