From what I understand, %04x pads the first element with 0s to make up 4 digits in hex, and is assigned to variable counter. After this, we print the rest of the line as a string.
However I cannot understand why we need counter+=NF in this case, as NF should be 16. Can you please explain this logic? Can we use something else to accomplish this (i.e for loop).
No. It's used as printf 's "format string" to be used for variable counter
YES, be aware of the strike through.
[quote]
However I cannot understand why we need counter+=NF in this case, as NF should be 16. Can you please explain this logic?
[quote]
The counter in fact is a pointer into the file, starting at the zeroth byte, then at the 16th, etc. Thus adding the line length (better: line's byte count) keeps the pointer pointing to the correct file position.
Interesting to know - when you say a pointer do you mean a pointer, say in the C programming language? Or just a pointer to the location.
In terms of for loop, I was wondering whether we could change the syntax to be more verbose like this:
awk -v counter=0000 'BEGIN { for (i = 1; i <= NR; ++i) printf "%04x %s\n, ", counter, $0; counter+=10}'
Additionally I'm not sure what part of the awk statement tells the first column to increment by 10, i.e. from 0000 to 0010. That's why I put a counter+=10 above.
Those 0000 and 0010 are hexadecimals, and the difference is 16. That's why I added NF which is the count of the 16 line elements. The scriptwould work with 14, 15, or 17 element lines as well.
Call it counter or pointer, it's an indicator of the position in file. A (typed) pointer in C is pointing to the memory location of a structure (type). Yes, there are similarities.