msk_0984 has asked for the wisdom of the Perl Monks concerning the following question:
Firstly, thanks for the prevous reply which i hav got for my recent post through which i was really able to learn new things. At Perl Monks site its really some thing like evolving day by day.
As over to the main topic, i am creating a script for auditing multiple log files. These log files contain data in the following format .....
I have the scenarios to audit this logs say : Need to get data from logs w.r.t Eg1: user -> sunny Host -> 192.168.1.93 module -> Remote Editing Eg2: user -> sunny Host -> All ( means from all hosts) module -> Remote Editing Similarly we have all the 8 secanrios for which i have written a script using if - else looping[Tue May 22 14:47:44 IST 2007][192.168.1.91][sunny][Distribution] Err +or: ct_connect(): directory service layer: internal directory control + layer error: Requested server name not found. <p> [Tue May 22 14:47:44 IST 2007][192.168.1.81][sridhar][PAD Manager] Wa +rning: Failback not supported. <p> [Tue May 22 14:47:44 IST 2007][192.168.1.93][sentini][Remote Editing] + Error: Results processing failed. <p>
This was working fine and i was able to get all the 8 scenarios which i was looking out for, but i felt it is not effective since i had to right 8 loops for this, i just want to know that rather than using 8 If -else looping any better effecient and effective way to do it.#! /usr/bin/perl my $sel_user,$sel_host,$sel_mod; print "\nEnter your choice for user: "; $sel_user=<STDIN>; chomp($sel_user); print "\nEnter your choice for host: "; $sel_host=<STDIN>; chomp($sel_host); print "\nEnter your choice for module: "; $sel_mod=<STDIN>; chomp($sel_mod); my $dir = './logs/commonlogs'; opendir my $dh, $dir or die "Can't opendir '$dir': $!\n"; my @files = grep { ! -d "$dir/$_" and ! /user.log/ } readdir $dh; foreach my $fname ( @files ) { #chomp($fname); $filename="$dir/$fname"; open(FH, $filename) or die "Error : $! \n" ; while($audit_data=<FH>) ### For greping out required data ####### +####################### { @check=split(']\[|]\s+|^\[',$audit_data); #print " $_ :: \n \n audit data $audit_data \n \n" foreach +(@check); if($check[3] =~ /^$sel_user$/ && $check[2] =~ /^$sel_host$ +/ && $check[4] =~ /^$sel_mod$/) { print " In first -- >> $audit_data"; } elsif($check[3] =~ /$sel_user/ && $check[2] =~ /$sel_host/ + && $sel_mod =~ /All/i) { print "In Second --- >> $audit_data"; } elsif($check[2] =~ /$sel_host/ && $check[4] =~ /$sel_mod/ +&& $sel_user=~ /All/i) { print "In Third -- >$audit_data"; } elsif($check[3] =~ /$sel_user/ && $check[4] =~ /$sel_mod/ +&& $sel_host=~ /All/i) { print "In Forth --> $audit_data"; } elsif($sel_user =~ /All/i && $sel_mod =~ /All/i && $check[ +2] =~ /$sel_host/) { print " In fifth -> $audit_data"; } elsif($sel_user =~ /All/i && $check[4] =~ /$sel_mod/ && $s +el_host =~ /All/i) { print " In sixth -> $audit_data"; } elsif($check[3] =~ /$sel_user/i && $sel_mod =~ /All/i && $ +sel_host =~ /All/i) { print " In seventh -> $audit_data"; } elsif($sel_user =~ /All/i && $sel_mod =~ /All/i && $sel_ho +st =~ /All/i) { print " In last -> $audit_data"; } } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Is there a Better work around rather than 9 If else Looping
by NetWallah (Canon) on May 23, 2007 at 13:36 UTC | |
by jdporter (Paladin) on May 23, 2007 at 18:20 UTC | |
Re: Is there a Better work around rather than 9 If else Looping
by blazar (Canon) on May 23, 2007 at 13:37 UTC | |
Re: Is there a Better work around rather than 9 If else Looping
by GrandFather (Saint) on May 23, 2007 at 21:34 UTC | |
Re: Is there a Better work around rather than 9 If else Looping
by Util (Priest) on May 23, 2007 at 22:21 UTC |