Simon
Simon

Reputation: 763

Increment array by 1 in awk

I try to use awk to increment my array by one with an "if" :

inf_a='1.0'
inf_b='4.0'
inf=($(seq $inf_a 1.0 $inf_b))

gro_a='0.0'
gro_b='4.0'
gro=($(seq $gro_a 0.5 $gro_b))

tri_a='1'
tri_b='12'
tri=($(seq $tri_a 1 $tri_b))

# counter
declare -A succ
declare -A fail
num_inf=${#inf[@]}
num_gro=${#gro[@]}
num_tri=${#tri[@]}

for ((i=1;i<num_inf;i++)) do
    ii=${inf[$i]}

    for ((j=1;j<num_gro;j++)) do
    jj=${gro[$j]}

        for ((k=1;k<num_tri;k++)) do
            kk=${tri[$k]}
            awk 'END{x=($2+$8);if($x<10) (( fail[$i,$j]++ )) ;else (( succ[$i,$j]++ )) }' infect$ii/$jj/trial$kk/out.dat
        done
        echo
    done
done
printf '%s\n' "fail[1,1]"
printf '%s\n' "succ[1,1]"

However, they both return 0. It seems that the problem originates from (( array[x,y]++ )). The "++" does not seem to work.

Is there any suggestion? Anything I missed?

Thanks.

Edited :

This is an example of my out.dat

47990   1451    234803  25  9816    2   593 1478    245212  605053  999.999695
47991   1451    234811  25  9806    2   593 1478    245210  605618  1000.010071
47992   1451    234821  25  9810    2   592 1478    245223  605928  1000.000610
47993   1450    234828  25  9812    2   593 1477    245233  605786  1000.003357
47994   1450    234826  25  9837    2   588 1477    245251  605642  1000.017029
47995   1450    234837  25  9832    2   588 1477    245257  605191  1000.104919
47996   1449    234849  26  9830    2   589 1477    245268  605350  1000.089966
47997   1449    234856  26  9831    2   588 1477    245275  605260  999.999695
47998   1447    234852  26  9834    2   589 1475    245275  605122  1000.124512
47999   1447    234838  26  9852    2   590 1475    245280  605124  999.999695

Edited : Modifying the code this way does not seem to work.

declare $( awk 'BEGIN{print "let "s=s+1""}' )

Upvotes: 0

Views: 362

Answers (1)

sat
sat

Reputation: 14949

There are some problems in your script.

  1. $i and $j is a bash variables. awk will not know their values. You have to use -v to export their values to awk.

  2. Then, fail[] and succ[] are awk arrays. How bash will know the values? Either you have print their values in awk and get it in bash using $() (Command substitution) and use it.

Refer below links:

How to use shell variables in an awk script

http://tldp.org/LDP/abs/html/commandsub.html

Upvotes: 1

Related Questions