LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-07-2008, 05:41 AM   #1
jmcmillan
LQ Newbie
 
Registered: Jun 2003
Posts: 13

Rep: Reputation: 0
Regular expression matching , match "error string" but not "other error"


Hello,
I've looked in the "similar threads" but couldn't find an answer.

Is there a regular expression I could use in a perl script that would match
"ERROR" but not "CRITICAL ERROR"?

In /var/log/messages we want to match things with "ERROR"
However HP's hpasmlited crashes occasionally and generates "CRITICAL ERROR". As far as I'm concerned they're not critical.

At present the regular expression is
"panic|halt|kern.error|ERROR"

I'd like to retain that functionality, only excluding "CRITICAL ERROR"

Please let me know if this isn't clear.

Regards - J McMillan
 
Old 07-07-2008, 06:28 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Code:
$ cat messages
some line with ERROR
they'll pay to know what they really think
a line with a CRITICAL ERROR oh noes!
another line with ERROR in it
pull the wool over your own eyes

$ perl -ne 'print if (/ERROR/ && !/CRITICAL ERROR/);' messages
some line with ERROR
another line with ERROR in it

$
 
Old 07-07-2008, 06:51 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
Does the line start with the words ERROR or CRITICAL ERROR or is this embedded within the line?

if yes then look at the start of line meta-character ^

otherwise you could try a negative assertion

(?<!CRITICAL )ERROR

Which says (I believe) match ERROR which is not preceded by CRITICAL<space>
 
Old 07-07-2008, 09:50 AM   #4
jmcmillan
LQ Newbie
 
Registered: Jun 2003
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks Matthew & Graeme.

I found that the "/ERROR/ && !/CRITICAL ERROR/" method gave me exactly what I required.

Thanks very much for both replies.
I verified it using a simple script

Code:
$ cat log
panic
halt
kern.error
ERROR
CRITICAL ERROR:  Resetting HP Advanced System Management Stack

$ cat perltest.pl
#!/usr/bin/perl -w

use strict;
open (IN, "log");
while (<IN>)
{
   if ((/kern.error|halt|ERROR|panic/) && (!/CRITICAL ERROR/))
   {
      print $_
   }
}
close IN;

$ ./perltest.pl
panic
halt
kern.error
ERROR
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error: Error for wireless request "Set Mode" (8B06) : invalid argument "roaming" penguin chick Linux - Wireless Networking 5 08-22-2008 01:16 PM
Regular expression to extract "y" from "abc/x.y.z" rag84dec Linux - Newbie 1 05-29-2008 02:47 AM
A single regex to match anything with ".aac" or ".mp3" at the end ? lumix Linux - General 9 05-09-2008 01:11 AM
Regular expression to match "^" then a number? PsychosisNode Linux - Newbie 1 01-14-2007 09:26 AM
unclean shutdown causes "Couldn't find matching filesystem: label=" error irnerdette Red Hat 3 06-02-2006 01:44 PM


All times are GMT -5. The time now is 06:50 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration