LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 03-18-2004, 07:29 PM   #1
prisoner
LQ Newbie
 
Registered: Jun 2003
Location: North Carolina
Distribution: Mandrake 9.1
Posts: 5

Rep: Reputation: 0
Question HELP ->PERL regex is kick'n my a...


I've just started using PERL. I like it. But what the heck is up with the regular expressions? My Kung-Fu is very weak.

I'm trying to write a script to slurp through a file and look for a line similiar to this:

Test Errors: 0

It's basically a log file. I only need to make sure the error number is 0 on this particular line of the file. There are other lines similiar to that one for example 'System Errors: blah', this makes it more confusinating for me. I just don't want to have to manually check file my app creates. So far I've been trying to do it like this:

open(myfile, "WHATEVER.FILE")
while (<myfile>) {
if ($_ = m/Test Errors: 0/) {
print "file is okay';
} else {
whatever

I know this doesn't work. I can't find a clear explanation of how to use regex for something a pattern similiar to mine. Can anyone help steer me in the right direction?
 
Old 03-18-2004, 07:31 PM   #2
prisoner
LQ Newbie
 
Registered: Jun 2003
Location: North Carolina
Distribution: Mandrake 9.1
Posts: 5

Original Poster
Rep: Reputation: 0
Sorry, I don't know if matters but there are like 12 or 13 whitespaces between Errors: and 0 in my file.
 
Old 03-18-2004, 09:49 PM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Try:

Code:
if (m/Test Errors: 0/g) {
   echo "No errors";
}
Or, if the number of spaces is variable, you can use something like

Code:
if (m/Test Errors:\s+0/g) {
   echo "No errors found"
}
Or, for something a bit more complex, if you wanted it to match for any number of errors and pull the number into a variable...

Code:
if (m/Test Errors:\s+(\d+)/g) {
   echo $1." Errors found.\n"

   if ($1 == 0) {
       echo "All is well\n"
   }
}
 
  


Reply



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
regex Perl help igotlongestname Programming 2 09-14-2005 07:51 PM
perl regex matching exodist Programming 2 11-15-2004 10:50 PM
perl: using 'substr' in regex ananthbv Programming 5 11-03-2004 01:58 AM
Regex help needed in perl Dr Twox Programming 2 08-07-2004 05:58 AM
perl regex question JustinHoMi Programming 5 03-20-2002 07:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:01 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration