Multi-Line Replacement Between Two Patterns

Sed: Mutli-Line Replacement Between Two Patterns

2018.05.18

This post has some useful sed commands which can be used to perform replacements and deletes between two patterns across multiple lines. For example, consider the following file:

$ cat file
line 1
line 2
foo
line 3
line 4
line 5
bar
line 6
line 7

1) Replace text on each line between two patterns (inclusive):
To perform a replacement on each line between foo and barincluding the lines containing foo and bar, use the following:

$ sed '/foo/,/bar/{s/./x/g}' file
line 1
line 2
xxx
xxxxxx
xxxxxx
xxxxxx
xxx
line 6
line 7

2) Replace text on each line between two patterns (exclusive):
To perform a replacement on each line between foo and barexcluding the lines containing foo and bar, use the following:

$ sed '/foo/,/bar/{/foo/n;/bar/!{s/./x/g}}' file
line 1
line 2
foo
xxxxxx
xxxxxx
xxxxxx
bar
line 6
line 7

3) Delete lines between two patterns (inclusive):
To delete all lines between foo and barincluding the lines containing foo and bar, use the same replacement sedcommand as shown above, but simply change the replacement expression to a delete.

 

$ sed '/foo/,/bar/d' file
line 1
line 2
line 6
line 7

4) Delete lines between two patterns (exclusive):
To delete all lines between foo and barexcluding the lines containing foo and bar, use the same replacement sedcommand as shown above, but simply change the replacement expression to a delete.

$ sed '/foo/,/bar/ {/foo/n;/bar/!d}' file
line 1
line 2
foo
bar
line 6
line 7

5) Replace all lines between two patterns (inclusive):
To perform a replacement on a block of lines between foo and barincluding the lines containing foo and bar, use:

$ sed -n '/foo/{:a;N;/bar/!ba;N;s/.*\n/REPLACEMENT\n/};p' file
line 1
line 2
REPLACEMENT
line 6
line 7

How it works:

/foo/{                   # when "foo" is found
  :a                     # create a label "a"
    N                    # store the next line
  /bar/!ba               # goto "a" and keep looping and storing lines until "bar" is found
  N                      # store the line containing "bar"
  s/.*\n/REPLACEMENT\n/  # delete the lines
}
p                        # print

6) Replace all lines between two patterns (exclusive):
To perform a replacement on a block of lines between foo and barexcluding the lines containing foo and bar, use:

$ sed -n '/foo/{p;:a;N;/bar/!ba;s/.*\n/REPLACEMENT\n/};p' file
line 1
line 2
foo
REPLACEMENT
bar
line 6
line 7

References:
Sed - An Introduction and Tutorial by Bruce Barnett

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值