How to print lines between two patterns, inclusive or exclusive (in sed, AWK or Perl)?

后端 未结 8 2283
生来不讨喜
生来不讨喜 2020-11-21 05:37

I have a file like the following and I would like to print the lines between two given patterns PAT1 and PAT2.



        
8条回答
  •  囚心锁ツ
    2020-11-21 05:56

    Here is another approach

    Include both patterns (default)

    $ awk '/PAT1/,/PAT2/' file
    PAT1
    3    - first block
    4
    PAT2
    PAT1
    7    - second block
    PAT2
    PAT1
    10    - third block
    

    Mask both patterns

    $ awk '/PAT1/,/PAT2/{if(/PAT2|PAT1/) next; print}' file
    3    - first block
    4
    7    - second block
    10    - third block
    

    Mask start pattern

    $ awk '/PAT1/,/PAT2/{if(/PAT1/) next; print}' file
    3    - first block
    4
    PAT2
    7    - second block
    PAT2
    10    - third block
    

    Mask end pattern

    $ awk '/PAT1/,/PAT2/{if(/PAT2/) next; print}' file
    PAT1
    3    - first block
    4
    PAT1
    7    - second block
    PAT1
    10    - third block
    

提交回复
热议问题