Question:
Is there a way to calculate a average of a single column stored in a text file? For example my file contains:
$ cat file.txt
line1 4.5
line2 6
how do I get 5.25 ?
Answer:
One way to do this is to use combination of bash for loop, cut, echo and bc commands. Execute the code below, assuming that file.txt is in your current working directory:
$ count=0; total=0; for i in $( awk '{ print $2; }' file.txt );\
do total=$(echo $total+$i | bc ); \
((count++)); done; echo "scale=2; $total / $count" | bc
5.25
and here is a shell script version of the above command so we can see what is happening in more detail:
#!/bin/bash

count=0;
total=0; 

for i in $( awk '{ print $2; }' file.txt )
   do 
     total=$(echo $total+$i | bc )
     ((count++))
   done
echo "scale=2; $total / $count" | bc
For each line in file.txt we extract a second column with awk ( $i ). Then we use echo and bc command to add all numbers $i to get a total $total. The script also stores a number of loops $count. The last line uses echo and bc commands to calculate average with two decimal points. AWK only method to calculate column average:
$ awk '{ total += $2; count++ } END { print total/count }' file.txt 
5.25
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed on LinuxCareers.com.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in:



Comments and Discussions