Finding existing occurances in a file...

Posted on 2003-02-20
Medium Priority
Last Modified: 2012-05-04
Just some problem I've encountered...
I have this text file where there are the following for multiple servers:

Server name: Longest downtime

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?

Question by:Maldini
  • 2
LVL 85

Expert Comment

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

Author Comment

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

Thx :)

Author Comment

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

Thx :)

Accepted Solution

amitabhrai earned 100 total points
ID: 7994257
$no_of_server = 0 ;
open ( SERVER , "server.txt" );
while ( <SERVER> ) {
     $line = $_;
     chop $line ;
     ( $ServerName , $longestDownTime ) = split ( /:/ , $line );
     $count_of_longestDownTime{ $longestDownTime } = $count_of_longestDownTime{ $longestDownTime } + 1;
     $Server_Count{$ServerName} = $longestDownTime ;
     $ServerNames[$no_of_server++] = $ServerName ;
close SERVER ;

open ( SERVER , ">server.txt" );
for ( $i= 0 ; $i < $no_of_server ; $i++ ) {
     if ( $count_of_longestDownTime{$Server_Count{$ServerNames[$i]}} > 1 ) {
          print SERVER "$ServerNames[$i]:$Server_Count{$ServerNames[$i]}:" . getAverage($ServerNames[$i]) . "\n";
     else {
          print SERVER "$ServerNames[$i]:$Server_Count{$ServerNames[$i]}\n" ;
close SERVER ;

sub getAverage
($name) = @_ ;
if ( $name eq "server1" ) {
     return "45"; }
else {
     return "60"; }

Change the subroutine "getAverage" to your subroutine and
change filename from "server.txt" to your file.
Hope this will work for you.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Article by: Tammy
MySQLTuner is a script written in Perl that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief forma…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question