Number calculations


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Number calculations
# 1  
Old 11-05-2014
Number calculations

I'm writing a script that will read all the fields of a text file into an array(if they are numeric), while at the same time computing the minimum and maximum values from the file. After that I want to output the average of all the numbers in the array.

The first problem I'm having is that many of the fields contain words, which I want to simply ignore. The current command I'm using is if($0 !~ /[a-z]/). This almost works, but if a record(line) begins with a word it stops reading from that line (even if numbers follow the word). Any help would be appreciated. Thanks.

Code:
{
if($0 !~ /[a-z]/)
{
        for (i=1; i<=NF; i++)

        if ($i > max)
        {
        max=$i
        }
else if ($i < min || min == 0)
        {
        min=$i
        }
}
}
        END{
        print "max = " max " and min = "min
}


Last edited by ksmarine1980; 11-05-2014 at 10:48 PM..
# 2  
Old 11-05-2014
This will clean out all non-digits (you need the . character for decimal numbers)

Code:
 tr -d '[:alpha:]' < infile > newfile

If you want better answers please post examples of sample input and expected output.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 11-06-2014
Thank you! I don't think I explained myself well. I'm not looking to delete any of the file I'm reading in from. I simply want to ignore any words and ONLY read in the numbers. Some of the files I'm trying to pull numerical data from are very large and aren't sorted so all of the words appear only on one column. I simply want to read in all the numbers while ignoring any words.

Example of the input file:
Code:
Josh 92 84 90 74
91 Richard 65 71 Simon 94
Michael 88 90 82 Sally 45
60 77 Tommy 90 82 81

I also tried
Code:
awk '/[[:digit:]]/' file.txt

and had the same problem.

Last edited by Franklin52; 11-07-2014 at 07:04 AM.. Reason: Adding code tags
# 4  
Old 11-06-2014
Try this:

Code:
{
   gsub(/[^[:space:]]*[^[:digit:][:space:]][^[:space:]]*/,x,$0)
   $1=$1
   for (i=1; i<=NF; i++) {
     if ($i > max) max=$i
     if($i < min || !min) min=$i
   }
}
END{
    print "max = " max " and min = "min
}

Here we are ignoring any word that contains non-digits think about some of these things you many on may not want to include:

Code:
12.75
$22.03
(05)3939-3934
IL-60607


Last edited by Chubler_XL; 11-06-2014 at 10:09 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 11-06-2014
Thank you, Chubler!

That did part of the trick, the only problem is the current text file I'm looking at has some negative numbers, which I need to keep.

Here's part of the input (which is an unsorted mess):
Code:
9 8
9
-10
88 9 12
0
16
22
Michael
15
to 85 9
-22
-9
17
Sam Richardson


Last edited by Franklin52; 11-07-2014 at 07:04 AM.. Reason: Please use code tags
# 6  
Old 11-06-2014
OK this should work for negative and decimal numbers:

Code:
{
   $1=$1
   for (i=1; i<=NF; i++) {
     if($i=="0" || $i+0 != 0) {
       if ($i+0 > max) max=$i+0
       if($i+0 < min || !min) min=$i+0
     }
   }
}
END{
    print "max = " max " and min = "min
}

# 7  
Old 11-06-2014
That did the trick! Again - thank you Chubler!!

Now to simultaneously populate an array with all these numbers and output the average value and count.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Loop doing calculations

Hello. I'm writing an awk script that looks at a .csv file and calculates the weighted grade for each student based on the scores and categories in the file. I am able to get the script to run the only issue however is that the same score for each student is the same. I'm self-teaching myself the... (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. Shell Programming and Scripting

Output calculations

Attached are the is original output (zipped file) and a custom file using the awk code below in which the average reads per bait are calculated (average.txt) awk '{if(len==0){last=$4;total=$6;len=1;getline}if($4!=last){printf("%s\t%f\n", last,... (7 Replies)
Discussion started by: cmccabe
7 Replies

3. Shell Programming and Scripting

Problem with calculations

grep Quality abc.txt | awk -F"=" '{print $2}' o/p is given as 70/70 49/70 I want in the below format (percentage format) 100% 70% help me!!!!:confused::confused::confused: ---------- Post updated at 09:59 AM ---------- Previous update was at 09:57 AM ---------- Cell 01 -... (3 Replies)
Discussion started by: nikhil jain
3 Replies

4. UNIX for Dummies Questions & Answers

Doing calculations with bc on one field

Hello, I have to turn: Apple Inc.:325,64:329,57 into Apple Inc.:325,64:329,57:3,93 3,93=329,57-325,64. My code: cat beurs.txt | sed 's/\(*\):\(*\),*\(*\):\(*\),\(*\)/\4\.\5-\2\.\3/' beurs.txt | bc| tr '.' ',' | sed 's/^-*,/0,/' > winstmarges.txt; paste -d: beurs.txt winstmarges.txt; rm... (5 Replies)
Discussion started by: ikke008
5 Replies

5. UNIX for Dummies Questions & Answers

help with doing calculations on data

Dear All, I have a long list like this: 337 375 364 389 443 578 1001 20100 . . . . etc I would like to substract each value from the first entry which in this case is 337 and report it in a separate column. So the expected output looks like 337 0 (10 Replies)
Discussion started by: pawannoel
10 Replies

6. Shell Programming and Scripting

calculations in bash

HI i have following problem, i need to use split command to split files each should be cca 700 lines but i dont know how to inplement it in the scripts becasuse each time the origin file will be various size , any body got any idea cheers (2 Replies)
Discussion started by: kvok
2 Replies

7. UNIX for Dummies Questions & Answers

Date Calculations

I need to be able to use the current date and calculate 7 days ago to be stored in another variable to be passed to a file in my Unix shell script. I need the date in the following format: date '+%m/%d/%Y' or 05/16/2006 How do I calculate date minus 7 days or 1 week ago? (8 Replies)
Discussion started by: mitschcg
8 Replies

8. Shell Programming and Scripting

ksh, calculations using bc

hi all, was wondering if there is another way to do calculations in ksh scripts other than using bc ?? i am using a script to calculate average response time and my script errors out after running for a bit. e.g code i am using : averageTime=$(print "$totalTime / $numberOfEntries" |... (2 Replies)
Discussion started by: cesarNZ
2 Replies

9. UNIX for Dummies Questions & Answers

Time Calculations

I'm trying to have a loop print out statistics every X number of seconds. How can I add a specific number of seconds to a time variable and make a comparison? Thanks ahead of time. For example: startTime = `date +%H%M%S` currentTime = $startTime executeTime = startTime + X # X is equal... (5 Replies)
Discussion started by: Nysif Steve
5 Replies

10. UNIX for Dummies Questions & Answers

Float calculations

As expr is used for integer calculations, which command is used for float calculations. (1 Reply)
Discussion started by: sharmavr
1 Replies
Login or Register to Ask a Question