Simon Simon - 2 months ago 9
Bash Question

Bash : check values in file

I try to check the end of my files for error checking. I try to work it by awk, but the values calculated in awk cannot be passed back to my bash.

s=0
f=0

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

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

for ((k=0;k<num_tri;k++)) do
kk=${tri[$k]}
awk 'END{x=($2+$8);if($x<10) (( f++ )) ;else (( s++ ))}' $ii/$jj/$kk/out.dat
done

printf '%2s' "$s"
printf '%2s' "$f"
s=0
f=0

done
done


The returned s and f are always 0. Since there is an if-else test here, I expect at least one of s, f returns non-zero value.

Does anyone has experience in using awk? I am a beginner in bash and awk. Or anyone has suggestion to bypass awk / has a simpler solution?

Thank you everyone.

Answer

here's an example of passing variables from bash to awk and back again:

result=$(awk -v f=$f -v s=$s '
  END{x=($2+$8);
  if($x<10) (( f++ ));
  else (( s++ ));
  printf "%d\t%d\n", $f,$s
}' $ii/$jj/$kk/out.dat)
f=$(echo "$result" | cut -f1)
s=$(echo "$result" | cut -f2)
Comments