Solved

doing math in a bash script.

Posted on 2010-08-27
3
524 Views
Last Modified: 2012-05-10
I'm grepping a file and getting a list of about a thousand numbers that I need to average and total.. The numbers are not big. The numbers I'm trying to add and average will be between 0 and 10000. What's the best way to average and add these numbers in a bash script?  I know I can send the list to a new file and read though it and do the math but I was hoping there might be a way to avoid creating a temp file. Any ideas?
0
Comment
Question by:jjc_mn
3 Comments
 
LVL 3

Accepted Solution

by:
egarciat earned 400 total points
Comment Utility
Something like this may help:

#!/bin/bash



SUM=0

ELEMENTS=0

AVG=0



cat myFile | while read line; do

if [ -n "$line" ]; then

let ELEMENTS++;

let SUM=$SUM + $line # let SUM+=$line?? not sure

fi

done



let AVG=$SUM/$ELEMENTS

echo $AVG

Open in new window

0
 
LVL 5

Assisted Solution

by:shajithchandran
shajithchandran earned 100 total points
Comment Utility
did you try using the built in command expr in the shell
0
 

Author Closing Comment

by:jjc_mn
Comment Utility
I'll give this a try when I can. Thanks!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Utilizing an array to gracefully append to a list of EmailAddresses
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now