Exponential function with bash, awk or similar

2.2k views Asked by At

I have this text file:

$ cat resultados.txt 
10.8
12.1
14.2
14.8
19.4
8.62
9.73

Important: numbers are in rows, not in columns.

I want to perform the geometrical operation:

e((l(number1)+l(number2)+l(numbern))/n)

How can I do that?

2

There are 2 answers

1
karakfa On BEST ANSWER

here is another alternative for geometric mean

$ awk 'BEGIN{p=1} {p*=$0; n++} END{print p^(1/n)}' file
12.3846

or, with NR

$ awk 'BEGIN{p=1} {p*=$0} END{print p^(1/NR)}' file
0
Ed Morton On

Your question would be clearer if you provided the expected output given that input but is this what you're looking for?

$ awk '{sum+=log($0)} END{if (NR) print sum, sum/NR, exp(sum/NR)}' file
17.6152 2.51646 12.3846