Average number of each line

Hi,

Please, can anyone help me how to get the average number of each line using awk

Input File:
6030 23 66 42
32 48 49 27 33
44 22 88 11 21
73 24 38 66 71

Desired Output:
44.2
37.8
37.2
54.4

Thanks

awk '{for(i=1;i<=NF;i++) s+=$i;printf("%.1f\n", s/NF)}' myFile

Thanks vgersh99,

Actually, I have written the code very similar to your code but it is not produce the desired output.

here, the output of your code:
44.2
82.0
119.2
173.6
awk: cmd. line:1: (FILENAME=test FNR=5) fatal: division by zero attempted

you must have an 'empty' line in your input file. try this:

awk 'NF {for(i=1;i<=NF;i++) s+=$i;printf("%.1f\n", s/NF)}' myFile

you have any empty line at the end of the file try the vgersh99 new code

Hi,

I have try your new code but the output does not show the average of each line unless only for the first line show the correct average

Input File:
6030 23 66 42 ---> Avg=44.2
32 48 49 27 33----->Avg=37.8
44 22 88 11 21 ---->Avg=37.2
73 24 38 66 71---->Avg=54.4

Desired Output:
44.2
37.8
37.2
54.4

my bad - sorry:

awk 'NF {s=0;for(i=1;i<=NF;i++) s+=$i;printf("%.1f\n", s/NF)}' myFile
1 Like

Hi vgersh99,

Now, your code is work

Thanks for your help....