
CPS
Novice
May 14, 2013, 1:38 AM
Post #1 of 4
(810 views)
|
RegEx patterns from array
|
Can't Post
|
|
Hello I would like to do a script which will: - opens a file with specific lines, reads it line by line - read regular expressions as parameters and save them in array - read opened file and test lines with regular expression from regex array. - if readed line doesn't match regex expressions - print this line into output - if it matches - print this too (optionally) In short, script needs to print only lines which doesn't match any of the regular expressions provided as parameters. It seems easy, however, i don't know how to do that. My current output is invalid beacause it gives me multiple lines. I know where the problem exists, but I don't know how to match both arrays and using regex. I used two foreach loops but it doesn't give proper results. Please help me with this newbie problem. Thanks.
# file to read # it has lines with events e.g. # [FILESYSTEM] FILESYSTEM - data 93% 90% KO # [TABLESPACE] TABLESPACE - 'ex2' BDD='test2' - 91.12% 80% (93.25Mo Free 1050Mo) KO # other... my $errFile = "c:\\KO\\test.txt"; open(FILE,$errFile); my @allExcludes; # processes to exclude (it will be as parameter, but for test purpose i put them in the variables my $processesToExclude = "some_process"; my $processesToExclude2 = "data"; my $processesToExclude3 = "ex2"; my $processesToExclude4 = "ex3"; my $processesToExclude5 = "ex4"; # if it's not null then put value into the allExcludes array. if ($processesToExclude ne "") {push(@allExcludes,$processesToExclude);} if ($processesToExclude2 ne "") {push(@allExcludes,$processesToExclude2);} if ($processesToExclude3 ne "") {push(@allExcludes,$processesToExclude3);} if ($processesToExclude4 ne "") {push(@allExcludes,$processesToExclude4);} if ($processesToExclude5 ne "") {push(@allExcludes,$processesToExclude5);} # how many - not needed now my $allExcludesSize = scalar (@allExcludes); my $line; my $wholeLine; # reading the file while (<FILE>){ $line = $_; chomp($line); $line =~ tr/\0-\037\177-\377//d; $wholeLine = "$wholeLine$line\n"; } # splitting line-by-line my @res = split(/\n/,$wholeLine); # all excluded my $allex; # all not excluded my $allnot; foreach $line(@res) { foreach $re (@allExcludes) { if ($line =~ /$re/) { $allex = "$allex$line\n"; #last; } else { $allnot = "$allnot$line\n"; #last; } } } print "match: \n$allex\n"; print "\n"; print "not match:\n $allnot\n";
|