James Edward Gray II <[EMAIL PROTECTED]>
01/20/2004 02:11 PM

 
        To:     "Anthony J Segelhorst" <[EMAIL PROTECTED]>
        cc:     [EMAIL PROTECTED]
        Subject:        Re: counters for lines


On Jan 20, 2004, at 12:39 PM, Anthony J Segelhorst wrote:

> I have the following output, and each value that is separated by comma 
> is
> a variable:
>
> servera,serverb,109,aix4-r1,server
> servera,serverb,109,aix4-r1,server
> servera,serverb,109,aix4-r1,server
> servera,serverb,109,aix4-r1,server
> servera,serverb,109,aix4-r1,server
> servera,serverb,109,hpux10,server
> servera,serverb,109,solaris2,server
> servera,serverb,109,solaris2,server
> servera,serverb,109,solaris2,server
> servera,serverb,109,solaris2,server
> servera,serverb,109,solaris2,server
> servera,serverb,109,solaris2,server
> servera,serverb,109,solaris2,server
> servera,serverb,109,solaris2,server
> servera,serverb,109,solaris2,server
> servera,serverb,109,solaris2,server
>
>
> I am trying to set up a counter that would actually output the data to 
> be:
>
> servera,serverb,109,aix4-r1,server,5
> servera,serverb,109,hpux10,server,1
> servera,serverb,109,solaris2,server,10
>
> Does anyone any suggestion on how to set up a counter to count the 
> lines
> until a different line shows up.  I already have the list sorted, so I
> just need to compare the new string to old string and if they are the 
> same
> increase the counter, and if they are different start a new counter.

>How about something like this (untested code):

>#!/usr/bin/perl

>use strict;
>use warnings;

>my($last, $counter) = (undef, 0);
>while (<>) {            # call with:  perl script_name FILE(S) TO COUNT > 

>OUTPUT_FILE
>                chomp;
>                if ($last) {
>                                if ($_ eq $last) { $counter++; }
>                                else {
>                                                print "$last,$counter\n";
>                                                undef $last;
>                                                $counter = 0;
>                                }
>                }
>                else {
>                                $last = $_;
>                                $counter++;
>                }
>}

>__END__

>James


I got this script written from James gave me:

$servers = "/tmp/ep_report_db_test1.log";
open (SERVERFILE, "<$servers");

my($last, $counter) = (undef, 0);
while (<SERVERFILE>) {           # call with:  perl script_name FILE(S) TO 
COUNT > OUTPUT_FILE
                chomp;
 
                if ($last) {
                                 if ($_ eq $last) { $counter++; }
                                 else {
                                                 print "$last,$counter\n";
                                                 undef $last;
                                                 $counter = 0;
                                 }
                 }
                 else {
                                 $last = $_;
                                 $counter++;
                 }
}


But now I am having problems counting if there is one record of that line? 
 Any suggestions?

Example:
servera,serverb,109,w32-ix86,client,1

But I am not getting a count of that line?


Anthony J Segelhorst
Enterprise Systems Management Team
Phone: 937-495-1876
Email: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service._______________________________________________________________




________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service._______________________________________________________________

Note:  Please update your email address for this user to reflect the
new MeadWestvaco Corporation.  MeadWestvaco employee email addresses
are in the format of [EMAIL PROTECTED] 

This electronic message contains information from MeadWestvaco
Corporation or subsidiary companies, which may be confidential,
privileged or otherwise protected from disclosure.  The
information is intended to be used solely by the recipient(s)
named.  If you are not an intended recipient, be aware that
any review, disclosure, copying, distribution or use of this
transmission or its contents is prohibited.  If you have
received this transmission in error, please notify MeadWestvaco
immediately at [EMAIL PROTECTED]
_______________________________________________________________________

Reply via email to