Solved

doing math in a bash script.

Posted on 2010-08-27
3
526 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
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 100 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

825 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