kittensfurdays
kittensfurdays

Reputation: 75

Shell Scripting and Averages

How would I find an average of a row in a file and output it in a specific way?

Say this is file1.docx

D 12-30-2006 12-30-2006 12-30-2006
B 15.8 16.1 15
P 30.1 29.6 29.9

and I want to make it so that I take row D and put it into a data module that reads like

Date    12/30/2006  ---refers to D
Tamb (°C)   15.63  ---refers to B 
Tref (°C)   29.87   ----refers to P

How would I go about this?

Upvotes: 1

Views: 25

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185881

$ awk '
    NR==1{print "Date "$2" refers to "$1"}
    NR>1{print "refers to "$1" Tamb (°C) " ($2+$3+$4)/3}
' file

Output :

Date 12-30-2006 refers to D
refers to B Tamb (°C) 15.6333
refers to P Tamb (°C) 29.8667

If you want to cut decimal part :

 $ awk '
     NR==1{print "Date "$2" refers to "$1"}
     NR>1{
         printf "%s", "refers to "$1" Tamb (°C) ";
         printf "%.2f\n", ($2+$3+$4)/3
     }
' file

Output :

Date 12-30-2006 refers to D
refers to B Tamb (°C) 15.63
refers to P Tamb (°C) 29.87

Upvotes: 1

Related Questions