LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 04-29-2010, 05:51 PM   #1
Alkass
Member
 
Registered: Mar 2010
Posts: 47

Rep: Reputation: 0
read the file, add number and get the average


Hi

I have a file which has several entries with

# comment 1

1245

# comment 2

5667

# comment 3

456456



how can I read the file, sum the numbers ( "avoid" the #lines) and then get the average of the numbers?

I do

awk '{s+=$0} END {print s}' /tmp/file.txt

and this give me the sum correctly, but how can I get also the average ?

Thanks in advance!

Last edited by Alkass; 04-29-2010 at 05:57 PM.
 
Old 04-29-2010, 06:03 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
If you first strip off everything but the numbers, then I think you can let AWK report the total number of lines.

Then just divide the total by the number of lines.
 
Old 04-29-2010, 06:31 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Try
Code:
awk '/^(#|$)/{next;}{++c;s+=$1} END {printf ("%d records: ",c);c=(c>0?c:1);print "sum: " s ", average: " s/c}' ' /tmp/file.txt
 
1 members found this post helpful.
Old 04-29-2010, 06:52 PM   #4
Alkass
Member
 
Registered: Mar 2010
Posts: 47

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by PTrenholme View Post
Try
Code:
awk '/^(#|$)/{next;}{++c;s+=$1} END {printf ("%d records: ",c);c=(c>0?c:1);print "sum: " s ", average: " s/c}' ' /tmp/file.txt
thanks!

Last edited by Alkass; 04-29-2010 at 06:53 PM.
 
Old 04-30-2010, 12:14 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please mark as SOLVED as you have your solution.
 
  


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
[SOLVED] average number of hosts that respond to pings during a traceroute icygalz Linux - Newbie 1 02-27-2010 03:21 AM
silo read error on block <number>... results in a short read peterlowrie Linux - Hardware 0 02-07-2010 07:39 PM
How to read numbers from a log file and to compute an average? 3ttt Programming 4 07-02-2007 03:08 AM
Read in an Octal number from a text file using C++ pjordan Programming 2 11-18-2004 03:03 PM
a script that go read the users names in a file and automaticly add then with a commo tumemanques211 Programming 2 03-22-2002 02:13 PM

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

All times are GMT -5. The time now is 11:13 AM.

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