Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

doing math in a bash script.

Posted on 2010-08-27
3
Medium Priority
?
530 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
egarciat earned 1600 total points
ID: 33545037
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 400 total points
ID: 33545082
did you try using the built in command expr in the shell
0
 

Author Closing Comment

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

664 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