Register
It is currently Wed Jul 30, 2014 11:22 pm

Need help with extracting numbers and finding the average


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sun Nov 01, 2009 10:04 pm   

Joined: Sun Nov 01, 2009 10:01 pm
Posts: 1
Hey all,

I ran some simulations, of which the output is 100s of files. I've used grep to extract the vital information needed from the files. This has made my task somewhat easier. But I still need to perform some mathematical calculations (average and geometrical average) on the results of the grep command (which is saved to a txt file).

The general format of the output of grep is (just a crude example):
Code:
a-4.txt number of eggs used is ## in #  cartons
b-4.txt number of eggs used is ## in #  cartons
b-6.txt number of eggs used is ## in #  cartons
a-6.txt number of eggs used is ## in #  cartons
a-8.txt number of eggs used is ## in #  cartons
b-8.txt number of eggs used is ## in #  cartons
a-10.txt number of eggs used is ## in #  cartons
b-10.txt number of eggs used is ## in #  cartons

My aim here is to get the average number of cartons (#) needed to pack ## number of eggs. Here I want to average all the a-4, a-6, a-8, and a-10 and then all the b-4, b-6, b-8, and b-10 cartons. SO my end result is to have an average number of cartons for both a and b. As you can imagine doing this by hand with results that can go up to 100 is very time consuming. Can someone please assist me with a BASH script that can make my job simpler?

Thank you.


Top
 Profile  
 PostPosted: Sun Nov 15, 2009 10:31 am   

Joined: Sun Nov 15, 2009 12:10 am
Posts: 2
to help u in the average calaculation

Code:
#the date will be in file called "input"


#  the average of all the number  for a will be in variable x
x=0;
num=0;
for i in $(cat input | grep "^a" | cut -d ' ' -f 7 ) ;
do
x=$(($x+$i));
num=$(( $num+1 ));
done  ;
x=$(( $x/$num))

#  the average of all the number  for b will be in variable x
x=0;
num=0;

for i in $(cat input | grep "^a" | cut -d ' ' -f 7 ) ;
do
x=$(($x+$i));
num=$(( $num+1 ));
done  ;
x=$(( $x/$num))


Top
 Profile  
 PostPosted: Sun Nov 15, 2009 7:08 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 533
What asks user009 is unclear (to me, at least), so I tried to make my script as flexible as possible
Code:
#!/bin/bash

infile=$(user009's grep output) #grep might not be useful as we can test line in the while loop

categories=($(cut -d'-' -f1 <<<"$infile" | sort -u))

count(){
nbline=nbline$1
nbCart=nbCart$1
   eval $nbline=$(( ++nbline ))
   eval $nbCart=$(( nbCart + $2 ))
}

while read LINE; do
   extr=$(cut -d' ' -f9 <<< $LINE)
   for category in ${categories[@]}; do
      test ${LINE:0:1} = $category && count $category $extr
   done
done <<< "$infile"

for i in ${categories[@]}; do
   echo "average cartons $i = $(( nbCart$i / nbline$i ))"
done


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP