Issue
INPUT FILE FORMAT
T12345670|1966-04-21|A01|1922-04-21|somebody name | |9999-01-01|
|CSGE|P175 |AGENT |AMGDHR01|1977-04-21|1977-04-21|2018-10-05|1977-04-21|0001-01-01| | |2018-11-01|2019-04-21| 2019.|01|D|M|PMI |D|0|N| 1
.| 1.|N| 1.| 1.|0| | ||N|0001-01-01| | 000000143.04| 000000075.00| 000000011.92| 000000011.92| 000000011.92| 000000034.14| 000000066.97| 000000130.04| 000005946.16|0|2018-10-05| 000000000.00|Y|2018-10-24|2018-10-23|499| 01.|||
I am trying to fix a syntax error. The following code prints the output just right.
awk -F'|' -v OFS=", " '{$61!=""} {k=substr($61,1,1); ks[k]; c[k,length($61)]++} END {for(k in ks) print k": "c[k,0],c[k,1],c[k,2],c[k,3],c[k,4],c[k,5],c[k,6],c[k,7],c[k,8],c[k,9],c[k,10],c[k,11],c[k,12],c[k,13],c[k,14],c[k,15]}' The_File.sqf | sort
Prints this:
0: , , , , , , , , , , 2010, , , , , 3010
1: , , , , , , , , , , 40, , , , , 44
3: , , , , , , , , , , 5, , , , , 1
4: , , , , , , , , , , 6, , , , , 3
5: , , , , , , , , , , 212, , , , , 92
7: , , , , , , , , , , 79, , , , ,
8: , , , , , , , , , , 966, , , , ,
: 1279330, , , , , , , , , , , , , , ,
C: , , , , , , , , , , , , , , , 2198
Cool, right?
QUESTION: now I am trying to clean that code up by using a NESTED FOR LOOP but I get a syntax error after trying many variations.
awk -F'|' -v OFS=", " '{$61!=""} {k=substr($61,1,1); ks[k]; c[k,length($61)]++} END {for(k in ks); do for(i in {1..15}); do print k": " c[k,i]}; done; done' TheFile.sqf | sort
ERROR:
The error context is
{$61!=""} {k=substr($61,1,1); ks[k]; c[k,length($61)]++} END {for(k in ks); do for(i in >>> { <<<
Solution
here I fixed it for you {$61!=""}
doesn't mean anything, so removed.
$ awk -F'|' '{k=substr($61,1,1); ks[k];
c[k,length($61)]++}
END {for(k in ks)
{printf "%s: ", k;
for(i=1;i<=15;i++) printf ", %d", c[k,i];
print ""}}' file | sort
Answered By - karakfa