LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-13-2006, 08:44 AM   #1
hta1984
LQ Newbie
 
Registered: Apr 2004
Location: Ankara
Distribution: suse
Posts: 9

Rep: Reputation: 0
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.
 
Old 12-13-2006, 09:34 AM   #2
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
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
 
Old 12-13-2006, 09:57 AM   #3
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
For security reasons (and also because its simpler) you should use mktemp
 
Old 12-13-2006, 11:12 AM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
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
 
Old 12-13-2006, 11:58 AM   #5
hta1984
LQ Newbie
 
Registered: Apr 2004
Location: Ankara
Distribution: suse
Posts: 9

Original Poster
Rep: Reputation: 0
thank you very much
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Show line and column numbers in Vim 7.0 judgex Linux - Desktop 1 08-29-2006 06:06 AM
Calculating age in days and month in a bash script jachba Programming 5 06-23-2006 01:37 PM
Can AbiWord show line and column numbers? MikHud Linux - Software 1 01-13-2006 09:15 AM
Bash scripting: column-ize file of varying length strings Quantum0726 Programming 4 08-13-2005 06:19 PM
need perl help calculating fibonacci numbers WorldBuilder Programming 5 12-17-2003 01:41 AM


All times are GMT -5. The time now is 11:22 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration