Find specific pattern and print complete text block using awk or sed

后端 未结 6 2193
离开以前
离开以前 2021-01-06 00:57

How can find a specific number in a text block and print the complete text block beginning with the key word \"BEGIN\" and ending with \"

6条回答
  •  被撕碎了的回忆
    2021-01-06 01:39

    A bit lenghty but the RS-trick was already posted :-)

    BEGIN {found=0;start=0;i=0}
    
    
    /BEGIN/ {
        start=1
        delete a
    }
    
    /.*567.*/ {found=1}
    
    {
        if (start==1) {
            a[i++]=$0
        }
    }
    
    /END/ {
        if (found) {
            for (i in a)
                print a[i]
        }
        found=0
        start=0
        delete a
    }
    

    Output:

    $ awk -f s.awk input
    BEGIN
    A: xyz
    B: 56789
    C: abc
    END
    BEGIN
    A: ghi
    B: 56712
    C: pqr
    END
    

提交回复
热议问题