Re: List and count routine (194 Views)
Reply
Occasional Advisor
user57
Posts: 12
Registered: ‎09-09-2009
Message 1 of 8 (194 Views)

List and count routine

I have a colon delimited file with usernames and claim codes.
I am looking for a routine to list the number of user claims per code.

For example, jones accesses to 2 codes A001 (twice) and A123 (once).
The desired output for the given input is displayed below:

input:
jones:A001
jones:A001
peters:A001
Peters:T909
nichols:A001
jones:A123


desired output:
jones (2) A001
jones (1) A123
peters (1) A001
peters (1) T909
nichols (1) A001

An awk solution would be helpful. Thanks
Please use plain text.
Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 2 of 8 (194 Views)

Re: List and count routine

Hi

These are all boolean AND conditions and many to many relationships. I don't think a shell script is suitable and I'm not good enough to answer in PERL. Wait a bit for JRF or another PERL guru's input on this.

In short, I could do but it'd be long and their are 3rd and 4th generation programming languages more suitable.
Support Fatherhood - Stop Family Law
Please use plain text.
Valued Contributor
Fredrik.eriksson
Posts: 237
Registered: ‎09-01-2008
Message 3 of 8 (194 Views)

Re: List and count routine

a pretty fast way would be to use the "uniq" feature :)

# uniq -c filename.txt
2 jones:A001
1 peters:A001
1 Peters:T909
1 nichols:A001
1 jones:A123

This can then be chopped up:
# uniq -c oop23i4p |replace ":" " "| awk {'print $2 " (" $1 ") " $3}'
jones (2) A001
peters (1) A001
Peters (1) T909
nichols (1) A001
jones (1) A123

Best regards
Fredrik Eriksson
Please use plain text.
Valued Contributor
Fredrik.eriksson
Posts: 237
Registered: ‎09-01-2008
Message 4 of 8 (194 Views)

Re: List and count routine

Replace "oop23i4p" with your filename, kinda just blabered something on the keyboard when creating the test file :P

don't assign me any points for this specific post

Best regards
Fredrik Eriksson
Please use plain text.
Honored Contributor
Raj D.
Posts: 2,624
Registered: ‎06-12-2004
Message 5 of 8 (194 Views)

Re: List and count routine

(awk /./|sort|uniq -c|awk -F: '{print $1,$2}'|awk '{print $2,"("$1")",$3}')
" If u think u can , If u think u cannot , - You are always Right . "
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 8 (194 Views)

Re: List and count routine

Hi:

In Perl:

# perl -nle '$object{$_}++;END{for $key (sort keys %object) {@a=split /:/,$key;print $a[0]," (",$object{$key},") ",$a[1]}}' file

Regards!

...JRF...
Please use plain text.
Honored Contributor
VK2COT
Posts: 921
Registered: ‎01-17-2006
Message 7 of 8 (194 Views)

Re: List and count routine

Hello,

You already got several excellent methods.

Here is another one in Perl.

#!/usr/bin/perl

use strict;

my @unique = ();
my @unique2 = ();
my %seen = ();
my @MyArr = ();
my @arr = ();
my $mycount = 0;
my %myhash;

while () {
chomp $_;
push(@MyArr, lc($_));
}

foreach my $elem (@MyArr)
{
$seen{$elem}++;
# my $result = sprintf("%-10s %s %5s\n", $arr[0], $arr[1], "($seen{$elem})");
$myhash{$elem} = "($seen{$elem})";
}

foreach my $hkey (keys %myhash) {
my @arr = split(/:/, $hkey);
my $result = sprintf("%-10s %5s %s\n", $arr[0], $myhash{$hkey}, $arr[1]);
# print "$hkey...$myhash{$hkey}\n";
print "$result";
}

exit(0);

__DATA__
jones:A001
jones:A001
peters:A001
Peters:T909
nichols:A001
jones:A123



When you run it, it formats the results as:

peters (1) a001
jones (2) a001
jones (1) a123
nichols (1) a001
peters (1) t909

Cheers,

VK2COT
VK2COT - Dusan Baljevic
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 8 (194 Views)

Re: List and count routine

Hi (again):

By the way, using my Perl solution means that the username "peters" differs from "Peters", Hence using your exact input will produce this output:

Peters (1) T909
jones (2) A001
jones (1) A123
nichols (1) A001
peters (1) A001

My script produces sorted output, too. Had you had "peters:A001" and "Peters:A001" these too would have been treated as discrete keys.

Regards!

...JRF...
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation