LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   calculating average of a column of numbers in bash (http://www.linuxquestions.org/questions/programming-9/calculating-average-of-a-column-of-numbers-in-bash-510025/)

hta1984 12-13-2006 09:44 AM

calculating average of a column of numbers in bash
 
Code:

#!/bin/bash
#sar.sh
i=1
while read myline
do
    if [ $i -eq 1 ]
    then
        echo "Report Date: $(cut -d" " -f4 ornek2-c.txt)" > output.txt
    elif [ $i -gt 3 ]
    then
        echo $myline > temp.txt
        proc=$(cut -d" " -f3 temp.txt)
        echo $proc > temp.txt
        cat temp.txt >> temp2.txt
    fi
    i=$(expr $i + 1)
done <ornek2-c.txt

index=$(expr $i - 4)
 
cat temp2.txt | awk '{ sum+=$1 } END {print sum}'
average=$(echo "scale=4; $sum/$index"|bc)
echo $average
exit 0

ornek2-c.txt: http://www.rafb.net/paste/results/AYouIq55.html

the output after execution(./sar.sh):

144
42.91
(standard_in) 1: parse error

i created ornek2-c.txt file with "sar -c -f ornek2.sa > ornek2-c.txt" command.i'm trying to find the average value of the proc/s column in ornek2-c.txt.

Thanks in advance.

makyo 12-13-2006 10:34 AM

Hi.

The variable "sum" is not defined in:
Code:

average=$(echo "scale=4; $sum/$index"|bc)
You could modify the previous line to:
Code:

sum=$( cat temp2.txt | awk '{ sum+=$1 } END {print sum}' )
which would then produce:
Code:

.3000
I removed the temp files before running and added a newline at the end of the last line of the input text file. If the newline is present, the script produces:
Code:

.2979
... cheers, makyo

nx5000 12-13-2006 10:57 AM

For security reasons (and also because its simpler) you should use mktemp

makyo 12-13-2006 12:12 PM

Hi, nx5000.

Good point, thanks. Yes, security issues are always important to keep in mind and mktemp is useful in that regard.

In this case, however, the reason for removing the files was more operational. The files were local, the script was appending to at least one file, and the results would be calculated incorrectly from the accumulation rather than the single dataset.

While I like the man page for mktemp because of the examples, I would say that one can as easily create filenames by using the $RANDOM value in bash, although one might still want to address the permissions issue. From my perspective the removal of temporary files, say with trap is at least as important as a random name. I don't reboot systems that often, I don't like the clutter in /tmp or elsewhere, and I also don't want to worry about accumulations as above.

Still, consistent use of mktemp or similar facilities can help with security particularly on shared systems ... cheers, makyo

hta1984 12-13-2006 12:58 PM

thank you very much


All times are GMT -5. The time now is 03:00 AM.