excluding first and last lines from sed /START/,/END/

后端 未结 5 1375
轮回少年
轮回少年 2020-12-24 01:18

Consider the input:

=sec1=
some-line
some-other-line

foo
bar=baz

=sec2=
c=baz

If I wish to process only =sec1= I can for example comment

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-24 01:23

    This should do the trick:

    sed -e '/=sec1=/,/=sec2=/ { /=sec1=/b; /=sec2=/b; s/^/#/ }' < input
    

    This matches between sec1 and sec2 inclusively and then just skips the first and last line with the b command. This leaves the desired lines between sec1 and sec2 (exclusive), and the s command adds the comment sign.

    Unfortunately, you do need to repeat the regexps for matching the delimiters. As far as I know there's no better way to do this. At least you can keep the regexps clean, even though they're used twice.

    This is adapted from the SED FAQ: How do I address all the lines between RE1 and RE2, excluding the lines themselves?

提交回复
热议问题