LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-22-2016, 10:52 AM   #1
bishop2001
Member
 
Registered: Apr 2015
Posts: 58

Rep: Reputation: Disabled
average max min bash


Greetings
im trying to get the average, max and min of a list of numbers although im seeing something strange. For very example i have the following values in a file and im running the following command: Although im getting the result with a non whole number which seems strange. Suggestions please ?
Thanks

Result:
average max min
0.151515 1 0

command:
awk '{if(min==""){min=max=$1}; if($1>max) {max=$1}; if($1<min) {min=$1}; total+=$1; count+=1} END {print total/count, max, min}'

file:
0
0
1
1
1
0
0
0
0
0
0
0
0
1
0
0
 
Old 01-22-2016, 10:59 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
With the exact same data and command I receive the following:
Code:
$ awk '{if(min==""){min=max=$1}; if($1>max) {max=$1}; if($1<min) {min=$1}; total+=$1; count+=1} END {print total/count, max, min}' file
0.25 1 0
So my questions would be to you:

1. Confirm that the data file has exactly those lines and no blanks?

2. What does awk point to on your computer? (for me it is gawk)
 
Old 01-28-2016, 07:52 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I concur with grail. My awk is /usr/bin/awk by the way, but the command works.

Debug a bit, not that the logic has any problems with it. But you can print out total and count. I'd check the file. I created my copy just using vi
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
The curious case of high 5 min load average jclyde Linux - Server 2 12-17-2015 06:51 PM
Trying to get max and min for each entry in a table kmkocot Linux - Newbie 3 11-28-2015 07:43 PM
5 min load average anirudh Programming 0 04-19-2005 09:04 AM
How to focus / restore / min / max windows with bash ? peb Linux - Newbie 2 02-29-2004 01:21 AM
Min/Max buttons? andykap Linux - General 3 12-15-2003 12:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration