awk range and selection of text

前端 未结 2 887
[愿得一人]
[愿得一人] 2021-01-23 09:49

I know how to use the range operator in awk

awk \'/start/,/stop/\' file

Is it possible to select text inside the range operator? ATM, I am usin

2条回答
  •  不思量自难忘°
    2021-01-23 09:53

    Never use a range expression as it makes trivial jobs very slightly briefer but then requires a complete rewrite or duplicate conditions when the task gets slightly more interesting.

    Instead of:

    awk '/start/,/stop/' file
    

    Use:

    awk '/start/{f=1} f{print} /stop/{f=0}' file
    

    and then what you want to do becomes simply:

    awk '/start/{f=1} f{ if ($1~/foo/) { } } /stop/{f=0}' file
    

    I'm assuming you have something in mind for inside the empty { }.

提交回复
热议问题