When learning to use awk, I found the expression awk 'c&&!--c;/regex/{c=N}' as a way to select and print the Nth line below the line matching the regex. I understand that c is initially equal to 0 so the first match isn't printed but despite having searched high and low, I don't know how to interpret the remaining syntax (outside of the /regex/) and how it specifically knows to count N lines before printing.
Can someone explain what c&&!--c means and how it works as a counter with the rest of the function?
The first half of the expression
cwill evaluate totrueifc != 0as you correctly guessed.The second half of the expression
!--cwill evaluate totrueif--cevaluates to0; this happens whenc==1immediately beforehand. Moreover, the expression will always decrementcas long asc != 0, soccan serve as a line counter.When the regular expression matches, we set
c == Nso that after exactlyNlines (each one decrementscby 1),c==1, andawkwill print the line.