Hi Team. I am trying to execute a simple for loop within an awk but its giving a different result. Below is the main code:
for
awk
awk '{for(i=1;i<=6;i++) print $i}'
The result should be 1 2 3 4 5 6 but its not giving this result. Can someone please help?
Try:
awk 'BEGIN{for(i=1;i<=6;i++) print i}'
$ does not mean variable in awk, it means column. So you were printing columns 1 through 6 of whatever line of data awk happened to be on at the time.
Note also that each invocation of print in awk will produce a separate line of output. If you want to print all of the values on a single line of output, you'll need to use printf instead of print and supply an appropriate format string argument.
print
printf