1834368 Members
2717 Online
110066 Solutions
New Discussion

Re: Need perl script

 
Swetha reddy
Occasional Contributor

Need perl script

data file looks like this:

key|value
10|03
11|23
10|23
11|32

need perl script with following output:

Number of unique keys: = 2

Total count of key 10 = 2 ( number of items)
Total count of key 11 = 2 (Number of items).





2 REPLIES 2
H.Merijn Brand (procura
Honored Contributor

Re: Need perl script

# perl -nle'm{^\s*(\d+)\s*\|} and $k{$1}++}END{print"Number of unique keys:",scalar keys%k;$k{$_}>1&&print"Total count of key$_ = ",$k{$_}"for keys%k' logfile

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
James R. Ferguson
Acclaimed Contributor

Re: Need perl script

Hi:

Use hashes like this:

# cat .hashit
#!/usr/bin/perl
while (<>) {
chomp;
@a = split /\|/;
$thing{$a [0]}++;
}
foreach $key (keys %thing) {
print "$key count = $thing{$key}\n";
}

...Run as ./hashit filename

Regards!

...JRF...