BASH. Need to extract some numbers and take the average


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH. Need to extract some numbers and take the average
# 8  
Old 11-03-2009
Code:
$ cat cartons
a-10.txt:number of eggs used is 55 in 4 cartons
a-4.txt:number of eggs used is 97 in 3 cartons
a-6.txt:number of eggs used is 22 in 1 cartons
a-8.txt:number of eggs used is 44 in 9 cartons
b-10.txt:number of eggs used is 87 in 5 cartons
b-4.txt:number of eggs used is 85 in 5 cartons
b-6.txt:number of eggs used is 36 in 6 cartons
b-8.txt:number of eggs used is 88 in 2 cartons

$ awk ' {if ($1~/^a/) {eggA+=$6;cartonsA+=$8}}; {if ($1~/^b/) {eggB+=$6;cartonsB+=$8}} END {print eggA, "eggs and " ,cartonsA, "cartons in a files, average is " eggA/cartonsA , "\n"  eggB,"eggs and ", cartonsB " cartons in b files, average is " eggB/cartonsB}' cartons
218 eggs and  17 cartons in a files, average is 12.8235
296 eggs and  18 cartons in b files, average is 16.4444


Last edited by rdcwayx; 11-03-2009 at 09:24 PM..
# 9  
Old 11-04-2009
Quote:
Originally Posted by Scrutinizer
Hi slackjack, the grep statement extracts those lines that contain the word 'cartons' from every file that is starts with 'a-' or with 'b-' The output is fed into the curly brace block (which is needed because in bash the fields will loose their values outside the while loop). The output is then fed into the read statement of which the first 5 fields and field 7 are discarded into a dummy variable called 'x'. Any further fields are all assigned to the last x.

If the line to look for in those files were "85 eggs in 5 cartons", the read statement would be:

Code:
read eggs x x cartons x

One other quick question. I'm having some difficulty finding the geometric average. I need to use exponents for this, and the result may floating point numbers at times. So for the eggs in a-X, the geometric average would be:
(no. of eggs in a-4 * no. of eggs in a-8 * no. of eggs in a-10) ^ (1/3)

How can we adjust the bash script to now find the geometric average?

Thanks again.

---------- Post updated 11-04-09 at 12:29 AM ---------- Previous update was 11-03-09 at 11:18 PM ----------

Quote:
Originally Posted by rdcwayx
Code:
$ cat cartons
a-10.txt:number of eggs used is 55 in 4 cartons
a-4.txt:number of eggs used is 97 in 3 cartons
a-6.txt:number of eggs used is 22 in 1 cartons
a-8.txt:number of eggs used is 44 in 9 cartons
b-10.txt:number of eggs used is 87 in 5 cartons
b-4.txt:number of eggs used is 85 in 5 cartons
b-6.txt:number of eggs used is 36 in 6 cartons
b-8.txt:number of eggs used is 88 in 2 cartons

$ awk ' {if ($1~/^a/) {eggA+=$6;cartonsA+=$8}}; {if ($1~/^b/) {eggB+=$6;cartonsB+=$8}} END {print eggA, "eggs and " ,cartonsA, "cartons in a files, average is " eggA/cartonsA , "\n"  eggB,"eggs and ", cartonsB " cartons in b files, average is " eggB/cartonsB}' cartons
218 eggs and  17 cartons in a files, average is 12.8235
296 eggs and  18 cartons in b files, average is 16.4444

Wow, awk looks like it will things easier with the arithmetic. Thanks for providing this.
# 10  
Old 11-04-2009
Hi slackjack, bash only knows integer arithmetic. However we can use external program (bc) for this:
Code:
#!/bin/bash
grep -h cartons [ab]-* |
{ while read eggs x x cartons x; do
    (( total_eggs+=eggs ))
    (( total_cartons+=cartons ));
  done
  printf '%s %.2F %s\n' "A total number of $total_eggs eggs in $total_cartons cartons makes an average of" $(echo "$total_eggs/$total_cartons"|bc -l) "eggs per carton"
}

Or you can use ksh93 which IMHO is a better shell for scripting purposes than bash and faster too. If you do not have it on your system you can download it for free. In ksh the same script would be
Code:
#!/bin/ksh
grep -h cartons [ab]-*|
while read eggs x x cartons x; do
  (( total_eggs+=eggs ))
  (( total_cartons+=cartons ))
done
printf '%s %.2F %s\n' "A total number of $total_eggs eggs in $total_cartons cartons makes an average of" $(( total_eggs/$total_cartons.0 )) "eggs per carton"

In both examples the value ".2" determines the precision and the output is:
Code:
A total number of 514 eggs in 35 cartons makes an average of 14.69 eggs per carton

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Substr with % - extract numbers only

# cat /etc/redhat-release Red Hat Enterprise Linux Server release 7.5 (Maipo) I have this script that will monitor filesystems and send me e-amil alerts. #! /bin/ksh DIST_LIST=monitor@...com WORKDIR=/home/monitor WARNLEVEL=90 MAIL_SUBJ="filesystems monitor on "$(hostname) ... (3 Replies)
Discussion started by: danielshell
3 Replies

2. Solaris

How is Load Average computed and what are better numbers?

Hello All, What is load average and how is it computed in Solaris 10? What are the different ranges for normal, warning and danger signs? Kindly clarify. Thank you, Sunil Kumar (3 Replies)
Discussion started by: msgforsunil
3 Replies

3. Shell Programming and Scripting

Extract numbers from file name-how to ?

Hello friends,I am new to Unix programming. how do I achieve the following in Unix shell script (I am running ksh on AIX) extract the number from name of file? My file format is like "LongFileName-1234.020614-221030.txt" now I want to extract value which is between (-) hyphen and (.) dot... (4 Replies)
Discussion started by: f150
4 Replies

4. Shell Programming and Scripting

awk or Bash: Cumulative average

For the data I would like to parse down and for each parsing I want a cumulative averaging, stored in an array that can be output. I.e. 546/NR = 546 (546+344)/NR=(546+344)/2 = etc. For N record input I want N values of the average (a block averaging effectively) Any... (3 Replies)
Discussion started by: chrisjorg
3 Replies

5. Shell Programming and Scripting

How to take a Average of numbers from different files?

Hi, I have 3 to 4 different files, from that i need to take a Average of numbers from a particular column. here i have to take 4th column, that should present in diff. file. File 1: Col1 col2 col3 col4 1 11 sa 12.00 2 22 sb 134.59 3 33 sc 11.99 4 44 sd 12.44 Col1 col2 col3... (8 Replies)
Discussion started by: Shenbaga.d
8 Replies

6. Shell Programming and Scripting

Bash script affect load average

Hello I have created next scritpt to do the next: chekp if host is alive. When the host down, launch telnet other equip to do checks. When execute the script the load average of the machines increase. For example: Before launch script top - 11:14:56 up 14 days, 18:06, 3 users, load... (3 Replies)
Discussion started by: capilla
3 Replies

7. Shell Programming and Scripting

Number of elements, average value, min & max from a list of numbers using awk

Hi all, I have a list of numbers. I need an awk command to find out the numbers of elements (number of numbers, sort to speak), the average value the min and max value. Reading the list only once, with awk. Any ideas? Thanks! (5 Replies)
Discussion started by: black_fender
5 Replies

8. UNIX for Dummies Questions & Answers

Taking a average of a column of numbers

Hey all, I am relatively poor at programming and unfortunately don't have time to read about programming at this current moment. I wanted to be able to run a simple command to read a column of numbers in a file and give me the average of those numbers. In addition if I could specify the... (2 Replies)
Discussion started by: Leonidsg
2 Replies

9. Shell Programming and Scripting

Extract numbers from text file work out average

Just wondering if someone could assist me with shell script I'm trying to write. I need to read the final column of a text file (shown below) and workout what the average number is. The text file will have a variable number of lines, I just want the script to pull out the values in the final field... (14 Replies)
Discussion started by: rich@ardz
14 Replies

10. Shell Programming and Scripting

extract numbers from a word

Hi ppl, I am a bit lost on this...can some one assist. I know this can be down with awk or sed, but i cant get the exact syntax right. I need to only extract the numbers from a signle word ( eg abcd.123.xyz ) How can i extract 123 only ? Thanks (14 Replies)
Discussion started by: systemali
14 Replies
Login or Register to Ask a Question