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

后端 未结 6 2283
离开以前
离开以前 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:36

    $ awk -v RS= -v ORS='\n\n' '/\nB: 567/' file
    BEGIN
    A: xyz
    B: 56789
    C: abc
    END
    
    BEGIN
    A: ghi
    B: 56712
    C: pqr
    END
    

    Note the \n before B to ensure it occurs at the start of a line.This is in place of the ^ start-of-string character you had originally since now each line isn't it's own string. You need to set ORS above to re-insert the blank line between records.

提交回复
热议问题