Explain awk command

后端 未结 5 505
谎友^
谎友^ 2020-11-28 06:29

Today I was searching for a command online to print next two lines after a pattern and I came across an awk command which I\'m unable to understand.

$ /usr/x         


        
5条回答
  •  心在旅途
    2020-11-28 06:51

    Wonderfully obscure. Will update when time allows.

    _ is being used as a variable name. The && is a logical operator that has 2 true actions run together. Once the value of _ is reduced to zero, the 2nd half of the && is false and no output is generated.

    print -- "
    xxxxx
    yyyy
    PATTERN
    zzz
    aa
    bbb
    ccc
    ffffd" | awk '_&&_--;/PATTERN/{_=2}'
    

    output

    zzz
    aa
    

    debug version

    print -- "
    xxxxx
    yyyy
    PATTERN
    zzz
    aa
    bbb
    ccc
    ffffd" | awk '_&&_--;{print "_="_;print _&&_};/PATTERN/{_=2;print "_="_ }'
    

    output

    _=
    0
    _=
    0
    _=
    0
    _=
    0
    _=2
    zzz
    _=1
    1
    aa
    _=0
    0
    _=0
    0
    _=0
    0
    _=0
    0
    

提交回复
热议问题