The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script to find the average number or files? bbbngowc Shell Programming and Scripting 2 03-27-2008 12:57 PM
Number Grouped Rows in File RacerX UNIX for Dummies Questions & Answers 2 03-03-2008 03:36 PM
Ability to store number of rows scabral Shell Programming and Scripting 2 03-29-2007 01:25 AM
Calculating the average kekanap Shell Programming and Scripting 2 01-26-2007 09:36 AM
return number of rows selected new2ss Shell Programming and Scripting 7 01-08-2007 10:46 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-15-2008
pk_eee pk_eee is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 18
Calculating the Number of Rows and Average

Hi All

I like to know how can we calculate the number of rows and the average of the values present in the file. I will not know what will be the rowcount, which will be dynamic in nature of the file.

eg.

29
33
48
30
28
  #2 (permalink)  
Old 08-15-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,813

Code:
awk '{ sum+=$1
         rowcnt++}
       END {printf("average: %.2f  rowcount:%d", sum, rowcnt) }' inputfile

  #3 (permalink)  
Old 08-15-2008
yahyaaa's Avatar
yahyaaa yahyaaa is offline
Registered User
  
 

Join Date: May 2008
Location: Jordan
Posts: 89
This is untested code..

nawk 'NR==1{k=0;Sum=0}{print $1; k=k+1; Sum=Sum+$1}END{print "Raws=" k " AVG.= "Sum/k} inputfile
  #4 (permalink)  
Old 08-15-2008
pk_eee pk_eee is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 18
Thanks...This works
awk 'NR==1{k=0;Sum=0}{print $1; k=k+1; Sum=Sum+$1}END{print "Raws=" k " AVG.= "Sum/k}' input File

But what iam looking for is
Output

<Name of the service> <AverageValue> <Number of Rows>
  #5 (permalink)  
Old 08-15-2008
yahyaaa's Avatar
yahyaaa yahyaaa is offline
Registered User
  
 

Join Date: May 2008
Location: Jordan
Posts: 89
could you provide us with example.... I dont really understand your request.. what do u mean by name of the service...
  #6 (permalink)  
Old 12-11-2008
grossgermany grossgermany is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 34
Hi Jim, what does %.2f mean?
  #7 (permalink)  
Old 12-11-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,423
Quote:
Originally Posted by grossgermany View Post
Hi Jim, what does %.2f mean?
display two places after decimal point
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:48 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0