0

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""}' )
Simon
  • 703
  • 2
  • 8
  • 19
  • 1
    The problem originates from trying to use shell inappropriately. A shell is an environment from which to call tools, not a tool to manipulate text. The whole thing should be done much more simply, clearly,robustly, and efficiently in a single awk script. – Ed Morton Sep 13 '16 at 05:28
  • 1
    Can you give us an example of your `out.dat`? – chw21 Sep 13 '16 at 06:27
  • chw21 : The file is now added. Thanks for the suggestion. – Simon Sep 14 '16 at 02:58

1 Answers1

1

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

Community
  • 1
  • 1
sat
  • 14,589
  • 7
  • 46
  • 65
  • 1
    If I try to pass a value into awk, then change the bash value, which direction should I look into? – Simon Sep 13 '16 at 06:22
  • 1
    @Simon, Sorry, I couldn't understand your question. – sat Sep 13 '16 at 07:19
  • The first link in your answer only talks about how to print a shell variable in awk. But what I need is changing a variable value in awk. I wonder if it works the same way. – Simon Sep 14 '16 at 02:49
  • @Simon, It should work. Ex: `i=2; out=$(awk -v svar=$i '{svar+=1; print $1, svar}' file)` . – sat Sep 14 '16 at 05:38
  • I try to change it for my code, but the code still returns 0. out=$(awk -v svar=$s '{svar+=1; print $1, svar}' infect$ii/$jj/trial$kk/out.dat) – Simon Sep 14 '16 at 05:58
  • I want to increment "s" by 1. I tried to do s=$out after the script, but it does not seem to work – Simon Sep 14 '16 at 07:53