LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-27-2008, 10:36 PM   #1
UnixKiwi
LQ Newbie
 
Registered: Dec 2006
Posts: 28

Rep: Reputation: 15
counting the number of unique items in a list


Hi
I'm trying count how many times each ID number occurs in a list

eg
list
1
2
1
3
4
1
4


output
1 3
2 1
3 1
4 2

I've managed to do it (with some help) in Perl but am wondering if it is possible to do it in awk (which I understand better)?

One possible complication is that the list will form the first column of a datafile but that would be easily solved by awk '{print $1}' data > temp

Many thanks in advance
UnixKiwi
 
Old 11-27-2008, 10:49 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you used Perl's hashes, you can do the same in awk using associative arrays
Code:
awk '{a[$1]++}END{for (i in a) print i,a[i]}' file
 
Old 11-27-2008, 11:16 PM   #3
UnixKiwi
LQ Newbie
 
Registered: Dec 2006
Posts: 28

Original Poster
Rep: Reputation: 15
thanks for that, it works perfectly and is much simpler than the perl script I had
 
Old 11-28-2008, 04:02 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Do they have to stay in their original order?
Code:
sort | uniq | grep -n .
ta0kira

edit: Sorry, I was going off of the title of the thread and didn't pay enough attention to the other posts. You actually ask two separate questions!

Last edited by ta0kira; 11-29-2008 at 06:46 PM.
 
Old 11-29-2008, 02:49 PM   #5
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Just for the record, this shouldn't be very hard using Perl:
Code:
#!/usr/bin/perl
use warnings;
use strict;

my %count;

while (<DATA>) {
  chomp;
  $count{$_} += 1;
}

foreach my $key (sort keys %count) {
  print "\t$key => $count{$key}\n";
}
__DATA__
1
2
1
3
4
1
4
Output
Code:
telemachus ~ $ perl count 
        1 => 3
        2 => 1
        3 => 1
        4 => 2
 
  


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
counting the output packet number zhoufanking Programming 4 07-06-2008 01:14 AM
Counting number of system reboots rbh123 Linux - Newbie 2 11-22-2007 03:28 AM
Trying to get unique system id (serial number) with FC4 dmattice Linux - Hardware 2 01-21-2006 05:42 PM
Unique identify motherboards (ID / serial number). How to? FredKroket Programming 2 05-12-2005 01:06 AM
counting number of files akin81 Linux - Newbie 6 03-25-2004 01:53 PM

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

All times are GMT -5. The time now is 07:58 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