Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

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

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

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

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, 07:28 AM   #2
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
$ 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, 07:51 AM   #3
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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


Which says (I believe) match ERROR which is not preceded by CRITICAL<space>
Old 07-07-2008, 10:50 AM   #4
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

$ cat log
CRITICAL ERROR:  Resetting HP Advanced System Management Stack

$ cat
#!/usr/bin/perl -w

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

$ ./


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

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 02:16 PM
Regular expression to extract "y" from "abc/x.y.z" rag84dec Linux - Newbie 1 05-29-2008 03:47 AM
A single regex to match anything with ".aac" or ".mp3" at the end ? lumix Linux - General 9 05-09-2008 02:11 AM
Regular expression to match "^" then a number? PsychosisNode Linux - Newbie 1 01-14-2007 10:26 AM
unclean shutdown causes "Couldn't find matching filesystem: label=" error irnerdette Red Hat 3 06-02-2006 02:44 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:19 PM.

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