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

后端 未结 5 1350
轮回少年
轮回少年 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:45

    If you're not interested in lines outside of the range, but just want the non-inclusive variant of the Iowa/Montana example from the question (which is what brought me here), you can write the "except for the first and last matching lines" clause easily enough with a second sed:

    sed -n '/PATTERN1/,/PATTERN2/p' < input | sed '1d;$d'

    Personally, I find this slightly clearer (albeit slower on large files) than the equivalent

    sed -n '1,/PATTERN1/d;/PATTERN2/q;p' < input

提交回复
热议问题