dru145 has asked for the wisdom of the Perl Monks concerning the following question:

Gracious Monks

I'm in need of your assistance once again. I have the top part of the script matching on any username that has been added to the database and storing them in the @certs array, but I want the users who's certificate has NOT been either revoked or renewed. In the below DATA, psmith.512 and mchin.2048 are the only users who match this criteria. Also, I need to return the whole line, not just the username.

I think I'm close, but I've been working on this all night and all day and still is not coming to me.

Any ideas on how I can get this working?

Thanks - Dru
#!/usr/bin/perl -w use strict; my $calog = "/var/log/ca/ca.log.1"; my $line; my @certs; my $i; my @newlist; open LOG, "$calog" or die "Can't open $calog: $!\n"; while ($line = <DATA>){ if ($line =~ m/\'([\w.]+)\' added/) { push(@certs, $1); } #end if for $i (@certs){ if ($line =~ m^/([\w.]+\.2048|512).+renewed|revoked/) { push(@newlist, $1); } #close if } #close for } #end while print "@newlist"; __DATA__ Sep 14 14:00:04 nt-na-pk NA(Domain-ca): Certificate came 'tuser.512' a +dded to database. Sep 14 14:00:04 nt-na-pk NA(Domain-ca): tuser.512: This Certificate ha +s been renewed. Sep 14 14:02:02 nt-na-pk NA(Domain-ca): [mless] Certificate came 'muse +r.2048' has been revoked. Sep 14 14:02:31 nt-na-pk NA(Domain-ca): [mless] Certificate came 'muse +r.2048' added to database. Sep 14 14:08:49 nt-na-pk NA(Domain-ca): Certificate came 'muser.2048' +added to database. Sep 14 14:48:20 nt-na-pk NA(Domain-ca): Certificate came 'psmith.512' +added to database. Sep 14 14:48:20 nt-na-pk NA(Domain-ca): Certificate came 'mchin.2048' +added to database.

Replies are listed 'Best First'.
Re: Need help with Parsing Unique Users out of Syslog
by dragonchild (Archbishop) on Sep 14, 2001 at 23:43 UTC
    Use a hash containing two items - what you want to use to determine if something is allowable and the whole line. How you build that hash will help you figure out what you're doing. :)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Need help with Parsing Unique Users out of Syslog
by HamNRye (Monk) on Sep 15, 2001 at 00:51 UTC

    Use a do...for loop.

    my $calog = "/var/log/ca/ca.log.1"; my @newlist; @calines = get_file($calog); @newlist = which_lines(@calines); print "@newlist"; ############################# # Important part ############################# sub which_lines { for (@_) { /\.512/ && do { push(@new1, $_);}; } for (@new1) { !/renewed|revoked/ && do { push(@new, $_); }; } return @new ; } sub get_file { my $file = shift; local *IN; open (IN, "<$file") or die "Cannot read '$file': $!"; if (wantarray) { return <IN>; } else { return join '', <IN>; } }

    This gives me the following output:

    Sep 14 14:00:04 nt-na-pk NA(Domain-ca): Certificate came 'tuser.512' a +dded to database. Sep 14 14:48:20 nt-na-pk NA(Domain-ca): Certificate came 'psmith.512' +added to database.

    It could be tidied up some, but you should get the idea....

    Update:

    After I posted this I re-read the problem, and this obviously doesn't work. You can change the /\.512/ to /\.512|\.2048/, and that takes care of the first problem I noticed.

    Now, you say users that have not been removed or revoked? Does this need to go chronologically? If not, parse the remaining good lines for your user names, and then re-search the original array for lines where they have been revoked or renewed.

    In the example of tuser.512, the renew line is directly after the good line. So in the case of chronological order, your logic flow could work: Get "added" line -> search next line for revoked|removed for same user.

    ~Hammy