I tried to use the FIELDWIDTHS parameter in nawk to specifiy my columns, but it isn't working quite right. Here is what I tried:
nawk 'BEGIN { FIELDWIDTHS = "3 8 4 10" } ; { arr[$1 $2 $3] += $4 } END {for (i in arr) {print i arr } }' count_sort.txt > count_sum.txt
It doesn't seem to be adding up the 4th column. based on the first 3 being equal. Also, is there a way to keep the leading spaces in the 4th column on the output?
Thanks, but I'm not sure what that printf is doing.
I was able to sort of get it to working by changing my code to :
nawk 'BEGIN { FIELDWIDTHS = "15 10" } ; { arr[$1] += $2 } END {for (i in arr) {print i arr } }' count_sort.txt > count_sum.txt
It looks like it is summing like I want, but it is removing the leading spaces for the last field. Any way to keep it from doing that?
---------- Post updated at 02:33 PM ---------- Previous update was at 02:07 PM ----------
Update -
I tried your printf suggestion. I think I got it to work using this:
nawk 'BEGIN { FIELDWIDTHS = "15 10" } ; { arr[$1] += $2 } END {for (i in arr) {printf "%15s%10s\n", substr(i, 1, 15), arr } }' count_sort.txt > count_sum.txt
I had to remove the first %s from your example. Otherwise it was giving me a not enough arguments for printf error. What was the %s doing in your example?