The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Count of matched pattern occurance palash2k UNIX for Dummies Questions & Answers 3 04-24-2008 12:33 PM
SED Search Pattern and Replace with the Pattern racbern Shell Programming and Scripting 4 03-15-2008 01:59 AM
How to count pattern in column ahjiefreak Shell Programming and Scripting 12 12-04-2007 03:53 PM
count the number chracters occurances in a line skyineyes Shell Programming and Scripting 6 06-26-2007 06:09 AM
Search file for pattern and grab some lines before pattern frustrated1 Shell Programming and Scripting 2 12-22-2005 11:41 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-09-2008
Registered User
 

Join Date: May 2008
Posts: 2
Stumble this Post!
nawk-how count the number of occurances of a pattern, when don't know the pattern

I've written a script to count the total size of SAN storage LUNs, and also display the LUN sizes.
From server to server, the LUNs sizes differ.
What I want to do is count the occurances as they occur and change.

These are the LUN sizes:
49.95
49.95
49.95
49.95
49.95
49.95
49.95
49.95
49.95
49.95
49.95
49.95
49.95
49.95
49.95
49.95
49.95
55.96
55.96
49.99
49.99
49.99
49.99
49.99
49.99
49.99
49.99
49.99

The above would produce something like this:
17x49.95, 2x55.96, 9x49.99

2nd example data file
27.96
27.96
27.96
27.96
27.96
27.96
27.96
13.95
2.95
2.95
142.99
142.99
142.99
142.99
142.99
142.99
142.99

produces
7x27.96, 1x13.95, 2x2.95, 7x142.99

I'm trying to find a way to do this with nawk.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-09-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 170
Stumble this Post!
Code:
nawk '{a[$0]++}END{for (i in a) print  a[i] ,i}'

For a fancier output use:

Code:
nawk '{a[$0]++}END{for (i in a) printf "%-2d -> %.2f \n", a[i], i}' file
Output:

Code:
17 -> 49.95 
9  -> 49.99 
2  -> 55.96
Reply With Quote
  #3 (permalink)  
Old 05-11-2008
Registered User
 

Join Date: May 2008
Posts: 2
Stumble this Post!
Thanks very much, that worked well. Very compact!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0