awk --> math-operation in a array

Hi

main object is categorize the difference of data-values (TLUFT02B - TLUFT12B).
herefor i read out data-files which are named
acording to the timeformat yyyymmddhhmm.

WR030B      266.48 Grad      0
WR050B      271.46 Grad      0
WR120B      268.11 Grad      0
WV030B        2.51 m/s       0
WV050B        3.47 m/s       0
WV120B        4.82 m/s       0
TLUFT02B      7.82 �C       0
TLUFT12B      6.87 �C       0
NSCHLB        0.00 mm        0

i try to read out the data and write it in a new file, with ...

#!/bin/bash
a=$(date --date '1 month ago' +%m)
e=$(date +%m)
y=$(date +%Y)
home=$HOME/Desktop/Messdaten/METE
cd $home
fpat=$(ls `eval echo $y{$a,$e}*` | tail -n 1584) ## 1584 Dateien entspr. 11 Tagen
for i in $fpat
do
      if test -f "$i"
      then
         Dy=${i:2:2} Dm=${i:4:2} Dd=${i:6:2}
        UZh=${i:8:2} UZm=${i:10:2}
        res=$(awk '{A[$1]=$2}END{print A["WV120B"], A["WR120B"], A["NSCHLB"], A["TLUFT12B"], A["TLUFT02B"]}' OFS='\t' "$i")
       T120=$(awk '{B[$1]=$2}END{print B["TLUFT12B"]}' "$i")
        T20=$(awk '{C[$1]=$2}END{print C["TLUFT02B"]}' "$i")
        printf "%s/%s/%s-%s:%s\t%s\t%s\n" $Dm $Dd $Dy $UZh $UZm "$res" "$T20-$T120"
      else
        DAT=$(date +%Y%m%d)
        printf "$DAT - Datei $i nicht vorhanden" >> "$home/../Logfiles/Log_$(date +%Y%m%d).log"
    fi
done

I want the difference of the values T20 and T120 -- blue!
but in the printf-comand gave me the listed values seperatet by the minus.
Please, can someone give me a hint
thanks in advance,
IMPe

You are printing string find out difference here

Integer

$ a=10; b=8
$ printf "%d\n" $(echo "$a-$b" | bc)
2

Floating point

$ a=10; b=8
$ printf "%f\n" $(echo "$a-$b" | bc)
2.000000

String

$ a=10; b=8
$ printf "%s\n" "$a-$b"
10-8

print as string after calculation

$ a=10; b=8
$ printf "%s\n" $((a-b))
2
1 Like

Instead of "$T20-$T120" , the proper syntax would be "$(( T20 - T120 ))"
--edit--
That should be $(echo "$T20 - $T120" | bc) since it is bash , not ksh93

1 Like

Thank you for your immediate help - i understand the mistake! Unfortunately it is not working well. i changed ...

printf "%s/%s/%s-%s:%s\t%s\t%f\n" $Dm $Dd $Dy $UZh $UZm "$res" "$T20-$T120" 

... the script printout gave me

line 17: printf: 17.10-17.02: invalid number

thanks in advance!
IMPe

it was supposed to be

$((T20-T120))

OR

$(echo "$T20-$T120" | bc)
1 Like

Thanks a lot! Sorry i've forgotten to tell, that i'm using bash. And with
$(echo "$T20 - $T120" | bc)
it's working fine.
how can i extend the echo-term. finally i want to check in which range the difference is. So is it between 0 an 1, then print A. Is it between 1 and 2 print B.
Is it possible to construct this with a case-statement?

Thanks in advance!
IMPe

As you are using awk anyhow, why don't you do the maths and formatting all in one single awk script? Might be clearer, cleaner and easier to maintain.

1 Like