Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Multiline Regex

by Cosmivalin (Initiate)
on Feb 15, 2001 at 23:30 UTC ( #58698=perlquestion: print w/replies, xml ) Need Help??

Cosmivalin has asked for the wisdom of the Perl Monks concerning the following question:

<html> <head> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>New Page 1</title> </head> <body>

Hi all, I have what's probably a simple question, but it stumping me. I am trying to develop a multi-line regex that will look through a text file and look for any lines that start with an ipaddress, but also the next line must contain "OS Type: unknown" and then insert a carriage return in front of the line with the ip address above that. I cant insert a CR in front of just any line that starts with an ipaddress. i.e.

10.1.1.1

bogus info

10.1.1.2

this could be anything

10.1.1.3

OS Type: unknown

10.1.1.4

filler information

10.1.1.5

OS Type: unknown

etc....

Any assistance would be appreciated... Thanks!

</body> </html>

Replies are listed 'Best First'.
Re: Multiline Regex
by kilinrax (Deacon) on Feb 15, 2001 at 23:37 UTC
    Asumming I've understood your question correctly, something like the following?
    #!/usr/bin/perl -w use strict; my $data = join '', <DATA>; $data =~ s|(?=^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\nOS Type: unknown$)| +\n|gm; print $data; exit(1); __DATA__ 10.1.1.1 bogus info 10.1.1.2 this could be anything 10.1.1.3 OS Type: unknown 10.1.1.4 filler information 10.1.1.5 OS Type: unknown
      Although the right solution is to use multiline mode, your code doesn't do anything as the test on the regex reveals. See here:
      #!/usr/bin/perl -w use strict; my $data = join '', <DATA>; if($data =~ s|(?=^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\nOS Type: unknown +$)|\n|gm) { print "yes, it worked!"; } #print $data; exit(1); __DATA__ 10.1.1.1 bogus info 10.1.1.2 this could be anything 10.1.1.3 OS Type: unknown 10.1.1.4 filler information 10.1.1.5 OS Type: unknown
      The above code returns nothing, meaning that the regex never does the substitution. From a quick rereading of the algorithm, it looks like he is trying to do the following:
      1. Read through the file
      2. find lines that start with IP addresses
      3. ASK if the next line following it has some specific text
      4. write an extra space BETWEEN the IP address line and the specific text line
      I may be wrong, as the spec. was pretty poorly written. A quick command line example that does this is as follows:
      perl -e "$q = qq(the brown dog\n is hailing a cab); if (($q=~m/\n(.*)/ +mg) && ($1 eq q( is hailing a cab))) { print qq(\n$1;)} else {print ' +no luck buddy.';}"

      Celebrate Intellectual Diversity

        Actually, the regex "mostly works".

        The problem with this regex is that the DATA does not have an immediate newline following it (there is a space). If the space is removed - or a simple (.+)? is added between the IP match and the newline, the first regex works fine. I'll leave someone else to do the benchmarks :-)

        That's of course, if the original poster wants the new line before the line with the IP

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://58698]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2022-07-01 20:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (102 votes). Check out past polls.

    Notices?