The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
urgent help in counting sunitachoudhury Shell Programming and Scripting 2 05-20-2008 10:44 PM
Counting up a variable Kenada Shell Programming and Scripting 3 07-18-2007 03:07 AM
Counting Processes heprox Shell Programming and Scripting 4 03-21-2007 05:44 AM
Help with counting files please gerard1 Shell Programming and Scripting 6 09-25-2006 01:37 PM
Counting LOC in C amatsaka High Level Programming 1 06-04-2002 03:18 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-23-2006
bobo bobo is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 148
Unhappy Counting and sort

Hello friends,

I have the following data:

abc-xyz
abc = 0-999
xyz = 0-999

and there are 10000 lines or more:

Example of data file:

123000
123001
123004
123005
123004
123004
123008
123008
123008
123008
246599
246700
246999
246999
246999
246999
246999
246888
246881
246999
357001
357888
357999
357999
357999
357999
357999
357999
357999
357999

I am looking for a script which will count the number of times the same number repeat. My result from the given data above should be:

123000 1
123001 1
123004 3
123005 1
123008 4
246599 1
246700 1
246881 1
246888 1
246999 6
357001 1
357888 1
357999 8

Thanks!
  #2 (permalink)  
Old 05-23-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
I'm not sure what the
Quote:
abc-xyz
abc = 0-999
xyz = 0-999
has to do with anything. but for your column of numbers try:
Code:
awk '
      {arr[$0]++}
      END { for(i in arr) 
              { print $i, arrr[i] }
      } '  filename > newfilename
  #3 (permalink)  
Old 05-23-2006
bobo bobo is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 148
Unhappy

Thanks for the help. It is not working: data range :0-999999

Data sample:

123000
123001
123004
123005
123004
123004
123008
123008
123008
123008
246599
246700
246999
246999
246999
246999
246999
246888
246881
246999
357001
357888
357999
357999
357999
357999
357999
357999
357999
357999

Expect data result from the script:

123000 1
123001 1
123004 3
123005 1
123008 4
246599 1
246700 1
246881 1
246888 1
246999 6
357001 1
357888 1
357999 8


Thanks!
  #4 (permalink)  
Old 05-23-2006
Glenn Arndt's Avatar
Glenn Arndt Glenn Arndt is offline Forum Advisor  
Anomalous Lurker
  
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
Code:
uniq -c myfile.txt
yields:

1 123000
1 123001
1 123004
1 123005
2 123004
4 123008
1 246599
1 246700
5 246999
1 246888
1 246881
1 246999
1 357001
1 357888
8 357999

If you really need to have the results like:

357888 1
357999 8

you can use a simple awk script:

Code:
uniq -c myfile.txt | awk '{print $2, $1}'
  #5 (permalink)  
Old 05-23-2006
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,190
There were just a couple of typos in Jim's solution, here is what he intended to post.

Code:
awk '
      {arr[$0]++}
      END { for(i in arr) 
              { print i, arr[i] }
      } '  filename > newfilename
Sponsored Links
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 10:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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