开发者

sed/awk : Remove lines from pattern to pattern

One of my configuration has a specific foo section:

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

I would like to apply changes to this section be replacing it with the contents of another file (e.g. new_foo.txt). The section always begins with # Configuration foo - Start and ends with # Configuration foo - Start

What's the best way to accomplish this in the Ubuntu\bash eco-system? I can write a sma开发者_开发技巧ll Python script, but I suspect there might be an elegant one-liner for this one.


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


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

If you put it all on one line then don't forget to put a ; and a space before the }.


Assuming new_foo.txt is small enough to be held in memory, this works for me:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜