How to understand the following awk command?
nawk 'a-- >= 0; /datamart_extractrelations_static/ {a = 30}' app.log
Given a awk program has a format of:
pattern { action }
What is 'a-- >= 0' do? when does this operation get executed?
The command is the same as:
nawk 'a-- >= 0 {print}; /datamart_extractrelations_static/ {a = 30}' app.log
If a-- >= 0
awk
will print the current line since print
is the default action in awk
which will be executed if no action was specified.
As a result, the command will print the 30ths line after a line that contains the pattern datamart_extractrelations_static
To better understand this, you can simply try the following command:
awk '1' input
Since 1
will always evaluate to true
and the default action is print
awk
prints every line of input unchanged.
Someone didn't quite get the idiom right for printing 30 lines after datamart_extractrelations_static is found in the input. See Printing with sed or awk a line following a matching pattern and look specifically at idiom e there "Print the N records after some pattern" for the correct code. The code you posted is missing the leading test on a
(who names a count variable a
???) to make sure the var doesn't keep decrementing below zero for a huge file and eventually wrap around and appear positive again.