Re: regex headache

2014-02-04 Thread Dr.Ruud

On 2014-02-03 21:30, Paul Fontenot wrote:

Hi, I am attempting to write a regex but it is giving me a headache.

I have two log entries

1. Feb  3 12:54:28 cdrtva01a1005 [12: 54:27,532] ERROR
[org.apache.commons.logging.impl.Log4JLogger]
2. Feb  3 12:54:28 cdrtva01a1005 [12: 54:27,532] ERROR [STDERR]

I am using the following
"^\w+\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2}\s+\w+\s+\[\d{1,2}:\s+\d{1,2}:\d{1,
2},\d{3}\]\s+\w+\s+\[[a-zA-Z0-9.]\]"

My problem is this greedy little '.' - I need to just be a period. How do I
match #1 and not match #2?



I think you should replace "\[[a-zA-Z0-9.]\]" by "\[[^]]+\]".

Don't worry of matching, see this as parsing, and skip a line on how it 
matches, not on how it doesn't match.


Hint: start using "named captures".

If you are into massively scanning log files, try MCE::Grep.

--
Ruud


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: regex headache

2014-02-03 Thread Jim Gibson

On Feb 3, 2014, at 12:30 PM, Paul Fontenot wrote:

> Hi, I am attempting to write a regex but it is giving me a headache.
> 
> I have two log entries
> 
> 1. Feb  3 12:54:28 cdrtva01a1005 [12: 54:27,532] ERROR
> [org.apache.commons.logging.impl.Log4JLogger]
> 2. Feb  3 12:54:28 cdrtva01a1005 [12: 54:27,532] ERROR [STDERR]
> 
> I am using the following
> "^\w+\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2}\s+\w+\s+\[\d{1,2}:\s+\d{1,2}:\d{1,
> 2},\d{3}\]\s+\w+\s+\[[a-zA-Z0-9.]\]"
> 
> My problem is this greedy little '.' - I need to just be a period. How do I
> match #1 and not match #2?


You appear to be making the job too difficult. The only difference between 
lines 1. and 2. is the last column. To differentiate those two, you can do this 
(assuming the string is in $_):

if( /\[STDERR\]/ ) {
  # process line 2
}else{
  # process line 1
}

Do you really need to match each field in the entire line? If so, I would try 
splitting the lines on whitespace and extracting the columns you need that way. 
Whether or not that works depends upon: 1) how much variation there can be in 
your log entries, and 2) what exactly you need to extract from each entry. 
Fixing that regex may not be the most productive approach in the long term.

As for your specific question, a period in a character class (e.g., [.]) will 
match a period. A period in the regex pattern will match any character (except 
possibly a newline). To match a period character, escape the period: /\./


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




regex headache

2014-02-03 Thread Paul Fontenot
Hi, I am attempting to write a regex but it is giving me a headache.

I have two log entries

1. Feb  3 12:54:28 cdrtva01a1005 [12: 54:27,532] ERROR
[org.apache.commons.logging.impl.Log4JLogger]
2. Feb  3 12:54:28 cdrtva01a1005 [12: 54:27,532] ERROR [STDERR]

I am using the following
"^\w+\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2}\s+\w+\s+\[\d{1,2}:\s+\d{1,2}:\d{1,
2},\d{3}\]\s+\w+\s+\[[a-zA-Z0-9.]\]"

My problem is this greedy little '.' - I need to just be a period. How do I
match #1 and not match #2?


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




RE: regex headache

2001-06-21 Thread Yacketta, Ronald



> Could you please explain what you're trying to accomplish in 
> a little more detail?  Are you trying to check whether either 
> string is found or only if both are found in the same line?
> 
both in the same line



Re: regex headache

2001-06-21 Thread Bradford Ritchie

Could you please explain what you're trying to accomplish in a little more detail?  
Are you trying to check whether either string is found or only if both are found in 
the same line?

> Folks,
> 
> I have been looking for a way to search for two strings in a line
> at the command line I would
> 
> Communication $grepTMPFILE | grep failure | wc -l
> 
> how would this be converted into perl/regex? I have part of it right now
> (Thanxs to others on the list)
> 
> foreach (@output) {
>   foreach my $test(@lookFor) {
> $results{$test}++ if /$test/igo;
>   }
> }

This looks like you want to count the number of lines that each element of @lookFor is 
found within @output.  However, the 'o' modifier in your regex will cause perl not to 
update the pattern each time through the loop.  That is, you'll loop through once for 
each element of @lookFor but the regex won't evaluate $test each time through the loop 
(only the first time.)  All elements of %results will have the same value!

> 
> I need to add a && to the if above.. correct?
> 
> if ( $test == 'Communication' ) {
>   if ( /$text/igo && /failed/igo ) { $results($test)++; }
> }

This looks like you're trying to find both strings occurring within the same line 
(assuming you're using the same foreach loops as above), but you'll still have the 
same trouble with the 'o' modifier.


Note that the 'g' modifier isn't necessary if all you want to do is increment the 
count for each line in which the pattern is found.  If you're trying to count all 
occurances of each element of @lookFor ... you'll have to ask the list (I though I 
knew but I just tested my assumption and it was wrong :)

*** Question: Does anyone know how to count the number of times a regex matches?  

  $count = @matches = /$pattern/g;

This works but seems like a waste.  
Does anyone know why m//g only returns 1 on success instead of returning the 
number of matches?


Thanks.
-- Brad




RE: regex headache

2001-06-21 Thread Yacketta, Ronald

Paul and I have already been down that road :)

right now, until I figure out a better way I have another perl script that
forks several processes egrepping data out.

I call this script from within my other perl script to populate @output,
yeah I should use open.. maybe I will once I get things working and output
as it should. I am currently working on a non production, non enduser box
so I can suck up as much cpu/mem/resources as I care ;)

Ron

> -Original Message-
> From: Michael Fowler [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, June 21, 2001 3:27 PM
> To: Yacketta, Ronald
> Cc: Beginners (E-mail)
> Subject: Re: regex headache
> 
> 
> On Thu, Jun 21, 2001 at 02:58:02PM -0400, Yacketta, Ronald wrote:
> > foreach (@output) {
> > foreach my $test(@lookFor) {
> > $results{$test}++ if /$test/igo;
> > }
> > 
> > this should create a hash etc.. etc..
> > to search for multiple words above (as you suggested?) I would
> > 
> > $results{$test}++ if /$test/i && /failure/i;
> 
> Ahh, I see.  Then yes, that would be the proper adaptation.  
> However, and
> someone may have already covered this with you, unless the file you're
> processing is guaranteed to always be small, sticking all of 
> it into @output
> is probably not the best of ideas.  If the output is that of 
> a program,
> recall my examples of using open for a program and filtering 
> the ouput.
> 
> 
> Michael
> --
> Administrator  www.shoebox.net
> Programmer, System Administrator   www.gallanttech.com
> --
> 



Re: regex headache

2001-06-21 Thread Michael Fowler

On Thu, Jun 21, 2001 at 02:58:02PM -0400, Yacketta, Ronald wrote:
> foreach (@output) {
>   foreach my $test(@lookFor) {
>   $results{$test}++ if /$test/igo;
> }
> 
> this should create a hash etc.. etc..
> to search for multiple words above (as you suggested?) I would
> 
> $results{$test}++ if /$test/i && /failure/i;

Ahh, I see.  Then yes, that would be the proper adaptation.  However, and
someone may have already covered this with you, unless the file you're
processing is guaranteed to always be small, sticking all of it into @output
is probably not the best of ideas.  If the output is that of a program,
recall my examples of using open for a program and filtering the ouput.


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--



RE: regex headache

2001-06-21 Thread Yacketta, Ronald

Doh!

ack! who chopped my line ;)

this
Communication $grepTMPFILE | grep failure | wc -l
should have been
grep Communication $grepTMPFILE | grep failure | wc -l

the actualy sub of the code I am using is

foreach (@output) {
foreach my $test(@lookFor) {
$results{$test}++ if /$test/igo;
}

this should create a hash etc.. etc..
to search for multiple words above (as you suggested?) I would

$results{$test}++ if /$test/i && /failure/i;


sorry for the confusion!



Re: regex headache

2001-06-21 Thread Michael Fowler

On Thu, Jun 21, 2001 at 01:43:44PM -0400, Yacketta, Ronald wrote:
> I have been looking for a way to search for two strings in a line
> at the command line I would

Well, for this you'd use something like:

open(FILE, $file) || die("Unable to open file \"$file\": \l$!.\n");
while () {
print if /$string1/ && /$string2/;
}


> Communication $grepTMPFILE | grep failure | wc -l
> 
> how would this be converted into perl/regex? I have part of it right now
> (Thanxs to others on the list)

Translating this verbatim:

open(COM, "Communication \Q$grepTMPFILE\E |") || die("fork failed: \l$!\n");
while () {
$count++ if /failure/;
}

print "$count\n";



> foreach (@output) {
>   foreach my $test(@lookFor) {
>   $results{$test}++ if /$test/igo;
>   }
> }
> 
> I need to add a && to the if above.. correct?
> 
> if ( $test == 'Communication' ) {
>   if ( /$text/igo && /failed/igo ) {  $results($test)++; }
> }


The three descriptions you provide above contradict each other.  You ask for
a solution to search for multiple strings in each line of a file, then show
an example of grepping the output of a program called Communication, then
list some Perl code doing neither.  Hopefully one of the solutions I've
provided solves your problem, but if not you need to be clearer about what
it is you're trying to solve.


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--



regex headache

2001-06-21 Thread Yacketta, Ronald

Folks,

I have been looking for a way to search for two strings in a line
at the command line I would

Communication $grepTMPFILE | grep failure | wc -l

how would this be converted into perl/regex? I have part of it right now
(Thanxs to others on the list)

foreach (@output) {
foreach my $test(@lookFor) {
$results{$test}++ if /$test/igo;
}
}

I need to add a && to the if above.. correct?

if ( $test == 'Communication' ) {
if ( /$text/igo && /failed/igo ) {  $results($test)++; }
}