Hello there,
I have been trying to get my awk script done and I am hanging in the last part for long hours. Just wish if someone could help me.
The actual op is:-
22 0 0 0
0 0 0 0
0 0 18 0
0 0 0 30
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Desired OP
21 14 17 29
22 0 18 30
23 0 19 31
24 0 0 32
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Input file array for this output is:-
#### Flavor,DonutValue #####
0,21
0,22
3,29
0,23
2,17
1,41
0,24
3,30
3,31
3,32
2,18
2,19
### A DOZEN COLLECTED ###
### LOOP COMPLETED ###
CODE SNIPPET.
!/bin/bash
for variable in c1 #c1 is the filename
do
no_of_dozen=0
awk 'BEGIN {RS="\n";FS=","}{
print "Line after awk =",$0
if ( $0 == "#### Flavor,DonutValue #####" ) {
no_of_dozen=$no_of_dozen+1
for (a=0;a<12;a++)
for (b=0;b<4;b++)
ar[b,a]=0
a1=0
a2=0
a3=0
a4=0
}
else if ( $0 == "### A DOZEN COLLECTED ###" ) {
for (a=0;a<12;a++)
print ar[0,a] "\t" ar[1,a] "\t" ar[2,a] "\t" ar[3,a]
}
else if ( $0 == "### LOOP COMPLETED ###" ) {
LOOP+=1
no_of_dozen=0
}
else if ( $0 == "" )
print "DO NOTHING"
else {
if ( $1 == 0 ) {
ar[0,$a1]=$2
print "ar_a1=" ar[$1,$a1]
print "a1=" a1
a1+=1
}
else if ( $1 == 1 ) {
ar[1,$a2]=$2
print "ar_a2=" ar[$1,$a2]
print "a2=" a2
a2+=1
}
else if ( $1 == 2 ) {
ar[2,$a3]=$2
print "ar_a3=" ar[$1,$a3]
print "a3=" a3
a3+=1
}
else if ( $1 == 3 ) {
ar[3,$a4]=$2
print "ar_a4=" ar[$1,$a4]
print "a4=" a4
a4+=1
}
}
}' $variable
done
Thank You all. Any guidance of why I am not able to do get this right will be helpful. I know this is not the best awk code as I have just started.
Cheers.
P.S. :The idea is to display in array format for columns with header of "0","1","2","3" and the corresponding numbers below it.