1752615 Members
5014 Online
108788 Solutions
New Discussion юеВ

PERL script help

 
Pat Peter
Occasional Advisor

PERL script help

I am once again found wanting in my scripting skills.

I have few files which have data like

tran_type tran_num Suc failure percentage

abc 11 10 5 50
def 12 10 0 100


there are around 10 to 15 files like this.

I need to write a script that would read all the files and combine the data together and generate a single output file containing the summation of all the tran type and number.

Can anyone please help.

-Pat


3 REPLIES 3
James R. Ferguson
Acclaimed Contributor

Re: PERL script help

Hi Pat:

Try this:

# cat perl.pl
#!/usr/bin/perl
use strict;
use warnings;
my ($key, $count);
my %data;
while (<>) {
($key,$count) = split;
$data{$key} += $count;
}
foreach $key (sort keys %data) {
print "$key $data{$key}\n";
}

Run as:

./perl.pl file1 file2 file3 ...

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: PERL script help

And of course you'd extent that to the other fields:

($key,$count,$suc,$fail) = split;
$data{$key} += $count;
$suc{$key} += $suc;
:, because you woudl have to do a weighted calc.
Just recalculate if needed after reading all.

Hein.
Hein van den Heuvel
Honored Contributor

Re: PERL script help

[ooop, wrong button. Moderator, please delete prior reply]

And of course you'd extent that to the other fields:

($key,$count,$suc,$fail) = split;
$data{$key} += $count;
$suc{$key} += $suc;
:
print ... $suc{$key}...

I woudl not add the percentages, because you would have to do a weighted calc.
Just recalculate if needed after reading all.

Hein.