Bash Sum Average numbers in files

0

I have a series of files with 2(two) lines. The first line contains a whole number (1-100) while the second line contains a decimal number(1.0000-99.9999). I need to get the sum of the first line and the average of the second line, i.e:

awk '{sum += $1} {avg / $2} NEXT {print sum} END {print avg}' *log

Example:

 1log
     20
     4.2
 2log
     34
     1.2

Where the output would look like this:

 54
 2.7

Ken J

Posted 2014-03-31T14:46:08.820

Reputation: 303

Answers

0

Whith awk, use the FNR variable, which hold the record number of the current file.

awk '
  FNR==1 {sum1 += $1}
  FNR==2 {sum2 += $1; count2++}
  END {print sum1; print (sum2/count2)}
' 1log 2log

or

paste 1log 2log | {
  read n1 n2; echo $((n1+n2))
  read n1 n2; bc <<< "($n1+$n2)/2"
}

glenn jackman

Posted 2014-03-31T14:46:08.820

Reputation: 18 546

This is failing for me, possibly because I have some files with null values. – Ken J – 2014-03-31T18:33:59.560