Reputation: 121
Data source:
a b c d
Expect:
$1 $2 $3 $4
a b c d
I try to echo 'a b c d'| awk 'END {for(i=1;i<=NF;i++) printf("$%d\t",i)}'
(but fail)
Upvotes: 0
Views: 38
Reputation: 203349
$ echo 'a b c d' |
awk 'FNR==1{for (i=1;i<=NF;i++) printf "$%d%s", i, (i<NF ? OFS : ORS)} 1'
$1 $2 $3 $4
a b c d
$ echo 'a b c d' |
awk 'FNR==1{for (i=1;i<=NF;i++) printf "$%d%s", i, (i<NF ? OFS : ORS)} 1' |
column -t
$1 $2 $3 $4
a b c d
Read Effective Awk Programming, 4th Edition, by Arnold Robbins - trying to do this in the END section and other things in your script indicate you are missing the fundamentals of awk.
Upvotes: 1