Explain awk command

后端 未结 5 506
谎友^
谎友^ 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 07:09

    _ is being used as a variable name here (valid but obviously confusing). If you rewrite it as:

    awk 'x && x--; /PATTERN/ { x=2 }' input
    

    then it's a little easier to parse. Whenever /PATTERN/ is matched, the variable gets set to 2 (and that line is not output) - that's the second half. The first part fires when x is not zero, and decrements x as well as printing the current line (the default action, since that clause does not specify an action).

    The end result is to print the two lines immediately following any match of the pattern, as long as neither of those lines also matches the pattern.

提交回复
热议问题