LinuxQuestions.org
Help answer threads with 0 replies.
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 02-07-2011, 03:20 PM   #1
sammywammy
LQ Newbie
 
Registered: Feb 2010
Posts: 7

Rep: Reputation: 0
Perl: Multiple line regex matching


I have a file that includes a number of lines with the string "Timeout 120".

I am aiming to find any double lines containing this same string.

I know little about Perl but did understand that I had to change the file separator $/ to undef in order to process the input file as one block.

The only problem is that the line below only returns the first occurrence of this double line and nothing else when there are multiple occurrences of these double lines all throughout the file.

perl -e '$/ = undef; while (<>) { if ($_ =~ m/Timeout 120\nTimeout 120$/mg) { print $&; }}' listener.txt


Does anybody see how this could be achieved using Perl?
 
Old 02-07-2011, 04:57 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911
Your snippet does exactly what it's meant to on my
machine; is the file you're looking at using CR\LF
by any chance, rather than LF?


Cheers,
Tink
 
Old 02-07-2011, 06:39 PM   #3
sammywammy
LQ Newbie
 
Registered: Feb 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Tinkster View Post
Your snippet does exactly what it's meant to on my
machine; is the file you're looking at using CR\LF
by any chance, rather than LF?


Cheers,
Tink
I don't think the problem with the file is that it is using CR LF as in \r\n since it is returning output but it is only returning the first occurrence of the double line rather than all of the many occurrences. The \n is being matched since it is returning something...

To be honest, I don't see why specifying the /m mode is not sufficient to get this to work. I must be missing something here...
 
Old 02-08-2011, 04:37 PM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 731

Rep: Reputation: 75
Hi.

Damian Conway in Perl Best Practices, p 240 ff, suggests that you always use xms on matches.

If you had posted some representative sample data in a CODE block, I would have tried it for you.

Best wishes ... cheers, makyo
 
Old 02-09-2011, 11:03 AM   #5
sammywammy
LQ Newbie
 
Registered: Feb 2010
Posts: 7

Original Poster
Rep: Reputation: 0
perl -ne '$/ = undef; while (/Timeout 120\nTimeout 120/g) { print $&."\n"; }' listener.txt

That seemed to do the trick. It matches all occurrences rather than just the first as the while condition depends on whether there is a match or not.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Perl regex not matching across multiple lines despite ms flags gfarrell Programming 30 08-18-2010 05:10 AM
perl matching multiple occurences on one line ShaqDiesel Programming 2 10-11-2009 08:13 AM
perl help for multiple line matching sal_paradise42 Programming 6 07-24-2008 08:35 PM
Embedded regex matching in Perl GATTACA Programming 5 01-17-2007 10:16 AM
perl regex matching exodist Programming 2 11-15-2004 11:50 PM

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

All times are GMT -5. The time now is 12:18 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration