Linux Tips and Tricks

Linux Tips and Tricks

Calculate Average of the column using bash and perl

September11

Create a file with some numbers

1
2
3
4
5
6
7
8
9
10
11
12
$ echo "5
6.5
8
9.2
> 13" > calculate.txt
 
$ cat calculate.txt 
5
6.5
8
9.2
13

Using AWK

1
2
$ awk '{sum+=$1}END{print sum/NR}' calculate.txt 
8.34

Using Perl

1
2
$ perl -lane '$a+=$_;END{print $a/$.}' calculate.txt
8.34
posted under Uncategorized

Email will not be published

Website example

Your Comment:


Recent Comments

    Categories