Awk is killing me.

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. :slight_smile:

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.

A sketch. If you don't have too many flavors:

cat INPUTFILE
0,21
0,22
3,29
0,23
2,17
1,41
0,24
3,30
3,31
3,32
2,18
2,19

awk -F, '{ print $2 > $1 }' INPUTFILE

# GNU sed  Change to literal tabs and -e ':a' -e '...' for an old one.
paste [0-9]* | sed  ':a s!\t\t!\t0\t!; ta'  
21	41	17	29
22	0	18	30
23	0	19	31
24	0	0	32

===

I don't see any easy way to do it with awk only. It would be easy but awkward with Perl. Python or Ruby or even Javascript are better.

Try this

#!/bin/bash
awk -F, ' {a[$1]=a[$1]" "$2}  
END{split(a[0],_0," ");split(a[1],_1," ");split(a[2],_2," ");split(a[3],_3," ")
i=1; while(i<=NR){printf _0+0"\t"_1+0"\t"_2+0"\t"_3[i++]+0"\n"}}'   input_file

--ahamed

Another one:

awk -F, 'END {
  for (i = 0; ++i <= NR;)
    for (j = mn; j <= mx; j++)
     printf "%d%s", d[++tt[j], j], \
       (j < mx ? OFS : RS)
  }
{
  d[++t[$1], $1] = $2
  $1 > mx && mx = $1
  (mn > $1 || $1 == 0) && mn = $1
  }' OFS='\t' infile

In some awk implementations NR is not available in the END block,
if you're using one of those, you'll need to save its value while reading the input file.

Advice taken... :slight_smile:

--ahamed