2011-07-19 75 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脚本,但我怀疑这个脚本可能会有一个优雅的单行程。

回答

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