BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Showing results for 
Search instead for 
Do you mean 

List and count routine

Highlighted
Occasional Advisor

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
7 REPLIES
Honored Contributor

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
Valued Contributor

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
Valued Contributor

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
Honored Contributor

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 . "
Acclaimed Contributor

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...
Honored Contributor

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
Acclaimed Contributor

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...