Link to home
Start Free TrialLog in
Avatar of Maldini
Maldini

asked on

Finding existing occurances in a file...

Hi,
Just some problem I've encountered...
I have this text file where there are the following for multiple servers:

Server name: Longest downtime

ie.
Server a: 150
Server b: 20
Server c: 150
Server d: 75

I want to scan the file and when there are servers with the same longest downtime, I need to add a new field for that record, being average downtime, and then place this edited line back into the text file.

Like taking server a and server c out so I can process them....into like
Server a: 150 (85)
Server c: 150 (70)

The average is calculated via another subprocedure so thats not the problem, its just the identification of those lines with the same "longest downtime" value that is posing some difficulties.

So can anyone show me how to find occurances with the same downtime, extract them for editing somehow so I may append the average onto the end of the line?

Thx
Maldini
Avatar of ozo
ozo
Flag of United States of America image

while( <> ){
    next unless my ($downtime)=/:\s*(\d+)/;
    push @{$down{$downtime}},$_;
}
while( my($k,$v) = each %down ){
    next unless @{$v} > 1;
    for( @{$v} ){
        print average $_;
    }
}
Avatar of Maldini
Maldini

ASKER

Could you please explain the second while loop? Since I'm new to perl and not too sure about how to use it..

Thx :)
Avatar of Maldini

ASKER

Could you please explain the second while loop? Since I'm new to perl and not too sure about how to use it..

Thx :)
ASKER CERTIFIED SOLUTION
Avatar of amitabhrai
amitabhrai

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial