Sponsored Content
Top Forums Shell Programming and Scripting BASH. Need to extract some numbers and take the average Post 302368016 by Scrutinizer on Wednesday 4th of November 2009 03:30:08 AM
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

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Carton(3pm)						User Contributed Perl Documentation					       Carton(3pm)

NAME
Carton - Perl module dependency manager (aka Bundler for Perl) SYNOPSIS
# On your development environment > cat cpanfile requires 'Plack', 0.9980; requires 'Starman', 0.2000; > carton install > git add cpanfile carton.lock > git commit -m "add Plack and Starman" # Other developer's machine, or on a deployment box > carton install > carton exec -Ilib -- starman -p 8080 myapp.psgi WARNING
This software is under heavy development and considered ALPHA quality till its version hits v1.0.0. Things might be broken, not all features have been implemented, and APIs are likely to change. YOU HAVE BEEN WARNED. DESCRIPTION
carton is a command line tool to track the Perl module dependencies for your Perl application. The managed dependencies are tracked in a carton.lock file, which is meant to be version controlled, and the lock file allows other developers of your application will have the exact same versions of the modules. TUTORIAL
Initializing the environment carton will use the .carton directory for local configuration and the local directory to install modules into. You're recommended to exclude these directories from the version control system. > echo .carton/ >> .gitignore > echo local/ >> .gitignore > git add carton.lock > git commit -m "Start using carton" Tracking the dependencies You can manage the dependencies of your application via cpanfile. # cpanfile requires 'Plack', 0.9980; requires 'Starman', 0.2000; And then you can install these dependencies via: > carton install The modules are installed into your local directory, and the dependencies tree and version information are analyzed and saved into carton.lock in your directory. Make sure you add carton.lock to your version controlled repository and commit changes as you update dependencies. This will ensure that other developers on your app, as well as your deployment environment, use exactly the same versions of the modules you just installed. > git add cpanfile carton.lock > git commit -m "Added Plack and Starman" Deploying your application Once you've done installing all the dependencies, you can push your application directory to a remote machine (excluding local and .carton) and run the following command: > carton install This will look at the carton.lock and install the exact same versions of the dependencies into local, and now your application is ready to run. Bundling modules carton can bundle all the tarballs for your dependencies into a directory so that you can even install dependencies that are not available on CPAN, such as internal distribution aka DarkPAN. > carton bundle will bundle these tarballs into local/cache directory, and > carton install --cached will install modules using this local cache. This way you can avoid a dependency on CPAN meta DB and search.cpan.org at a deploy time, or you can have dependencies onto private CPAN modules aka DarkPAN. COMMUNITY
<https://github.com/miyagawa/carton> Code repository, Wiki and Issue Tracker <irc://irc.perl.org/#carton> IRC chat room AUTHOR
Tatsuhiko Miyagawa COPYRIGHT
Tatsuhiko Miyagawa 2011- LICENSE
This software is licensed under the same terms as Perl itself. SEE ALSO
cpanm Bundler <http://gembundler.com/> pip <http://pypi.python.org/pypi/pip> npm <http://npmjs.org/> perlrocks <https://github.com/gugod/perlrocks> only perl v5.14.2 2012-04-12 Carton(3pm)
All times are GMT -4. The time now is 12:59 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy