grep lines matching a pattern, and the lines before and after the matching until different pattern

前端 未结 4 2079
我寻月下人不归
我寻月下人不归 2021-01-22 02:21
Start_pattern
abc
d End_pattern
Start_pattern
abc
d
ef
ghij 
klm
no End_pattern
Start_pattern
abc
def
hij End_pattern
Start_pattern
abc
dhi
jklm End_pattern
4条回答
  •  旧时难觅i
    2021-01-22 02:40

    $ cat tst.awk
    /Start_pattern/ { fnd=1; buf="" }
    fnd {
        buf = buf $0 ORS
        if (/End_pattern/) {
            if (buf ~ /ef/) {
                printf "%s", buf
            }
            fnd = 0
            buf = ""
        }
    }
    
    $ awk -f tst.awk file
    Start_pattern
    abc
    d
    ef
    ghij
    klm
    no End_pattern
    Start_pattern
    abc
    def
    hij End_pattern
    

提交回复
热议问题