Printing all contents EXCEPT matching range pattern using awk

前端 未结 3 1951
滥情空心
滥情空心 2021-01-13 11:12

In Awk, the range pattern is not an expression, so canot use the \"!\" to not it. so how to implement it (Printing all contents EXCEPT matching range pattern using awk)?

3条回答
  •  清歌不尽
    2021-01-13 12:00

    If you really need to use awk, something like this should work:

    awk 'BEGIN{x=1} /startpattern/{x=0} /endpattern/{x=1;next} x{print}'
    

    Although the sed alternative might be a simpler approach (it's less typing, at least).

    Edit: @Kent pointed out you have the same start and end pattern, which makes it a bit more tricky, but this should work:

    awk 'BEGIN{x=1} /pattern/{x=!x;next} x{print}'
    

    That basically toggles x every time it sees the pattern, and only prints when x!=0. The next in there avoids printing the pattern when it's turning printing back on.

提交回复
热议问题