CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
RegEx patterns from array

 



CPS
Novice

May 14, 2013, 1:38 AM

Post #1 of 4 (401 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.


Code
# 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";



Laurent_R
Veteran / Moderator

May 14, 2013, 4:26 AM

Post #2 of 4 (397 views)
Re: [CPS] RegEx patterns from array [In reply to] Can't Post

Your code is far far too complicated. The whole thing could be done in less that 10 lines.

The logic of your if/else within the inner foreach loop is probably faulty (unless I misunderstood your requirement). Don't do last and don't concatenate the not matchnig line if it did not match the first regex. You need to check each regex et concatenate the non martching line only after that, if none matched.


BillKSmith
Veteran

May 14, 2013, 5:54 AM

Post #3 of 4 (390 views)
Re: [CPS] RegEx patterns from array [In reply to] Can't Post


Code
use strict; 
use warnings;
my $opt = 0;
my @processesToExclude = @ARGV;
my $RE = join '|', @processesToExclude;
while (my $line = <DATA>) {
$line =~tr/\0-\037\177-\377//d;
print "$line\n" if $line !~ m/$RE/o or $opt;
}
__DATA__
hide data
show this
hide ex2 stuff
display line
do not show ex4

Good Luck,
Bill


CPS
Novice

May 14, 2013, 8:22 AM

Post #4 of 4 (384 views)
Re: [BillKSmith] RegEx patterns from array [In reply to] Can't Post

Thanks for the all replies :)

BillKSmith <- yes, i've found join function as well by myself. it resolves my issue.

Thanks again.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives