in reply to Re^2: search and extract lines which contain a word
in thread search and extract lines which contain a word

Hi Experts, Forget my last question post. I've managed to get it done. Not sure if i got it done 'intelligently' though. =P Can anyone please advice on this instead? Based on the current code below, how do i EXCLUDE extracting lines that contain any of the array of names defined, although strings 'gateway' & 'timestamp' are found in the particular line? E.g. ## To exclude extracting lines with username 'andrew' ## dcndksckdsnckdc gateway jdscjscjsdh timestamp andrew dkcd
print "Specify full directory of logfile.\n"; $file = <STDIN>; chomp $file; print "\nIdentified file is $file\n"; print "Is this correct? (y/n)\n"; $confirm = <STDIN>; chomp $confirm; if ($confirm eq "y") { print "\nCommencing work on $file.\n"; workings(); } else { print "Terminating program...\n"; } ## Subroutine to open & read logfile sub workings { print"In Subroutine workings now.\n"; open (LOGFILE, $file); print "Opening logfile...\n"; open (OUT, ">>", 'D:\Temp\test.txt') or die "$!"; print "Opening output file...\n"; print "Checking for string GATEWAY...\n"; while (<LOGFILE>) { if (/gateway/) { $count = $count + 1; print OUT; print "Extracting line ...\n"; } elsif (/TIMESTAMP/) { $count = $count + 1; print OUT; print "Extracting line ...\n"; } } print "\nTotal of $count lines extracted.\n"; close OUT; close LOGFILE; } ##Array to contain usernames @names = qw(mddinzam khairulz sawaikha caoyx jchew khamshae hartinib t +eev xiaolh yettynm yussofyu leegm narayam karuppa doraira edanor raza +liha sambanr jwong lamks linwb rashid ongsp ooisc mohdrosn);

Replies are listed 'Best First'.
Re^4: search and extract lines which contain a word
by ikegami (Patriarch) on Mar 27, 2008 at 11:36 UTC

    That prints out the lines containing either or both of "gateway" and "TIMESTAMP". Your if is equivalent to

    if (/gateway/ || /TIMESTAMP/) { $count = $count + 1; print OUT; print "Extracting line ...\n"; }

    However, I was under the impression you want lines containing both. If you want the lines containing both, your if would look like

    if (/gateway/ && /TIMESTAMP/) { $count = $count + 1; print OUT; print "Extracting line ...\n"; }

    As for excluding the listed names, one way is to start by building a regex using one of the following two methods:

    my ($exclude_re) = map qr/$_/, join '|', map quotemeta, @names;

    or

    use Regexp::List qw( ); my $exclude_re = Regexp::List->new()->list2re(@names);

    The just make sure the line doesn't match that regexp:

    if (/gateway/ && /TIMESTAMP/ && !/$exclude_re/) { $count = $count + 1; print OUT; print "Extracting line ...\n"; }