Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Matching string, then getting next line

by inman (Curate)
on Mar 08, 2006 at 10:39 UTC ( #535133=note: print w/ replies, xml ) Need Help??


in reply to Matching string, then getting next line

Hi minixman

It would be worth trying a slightly different approach to the problem. You have mentioned that your content is coming from a file, it would be easier to work with the file itself line by line, look for the match and then do something with the next line. e.g.

while (<MYFILE>){ chomp; #remove line feed if (/do the match here/) { my $nextLine = <MYFILE>; # do stuff with the next line here. } }

This reads the content of the file handle, line by line placing each line into the default $_ variable. You can test to see if you have a match and then take another line from the file.

If you have all of your content in a list then you can substitute the reading of a line from the file with a shift of a line form the list.


Comment on Re: Matching string, then getting next line
Download Code
Replies are listed 'Best First'.
Re^2: Matching string, then getting next line
by minixman (Beadle) on Mar 08, 2006 at 11:09 UTC
    Hi THe problem with that is, that when perl reads my file which was created on windows, it treats the whole file as one line, that is why i had to use FILE::Slurp to split out the lines. So what i need to do is say okay if you match my line, place the value into a variable and then get the next line whatever it is and place that into a variable.
      I guess that you are trying this with a file that has been generated on Windows and then transferred to a UNIX box. You end up with extra ^M chars when you look at the file using vi. Try trimming the surplus whitespace using a regex
      #!/usr/bin/perl -w use strict; while (<MYFILE>){ s/\s+$//; if (/NEXT LINE MARKER/) { my $nextLine = <MYFILE>; $nextLine =~ s/\s+$//; print "$nextLine\n"; # do stuff with the next line here. } }
        That does work, but it splits out the lines that i am looking for so when i run it i end up with.
        #!/usr/bin/perl open(MYFILE, "log.log"); while (<MYFILE>){ s/\s+$//; if (/Message\sdump:/) { my $nextLine = <MYFILE>; $nextLine =~ s/\s+$//; print "Found=$nextLine\n"; # do stuff with the next line here. } }

        Found=23 06/02/23 11:19:34:750 Received FIX message Found=8=FIX.4.29=024935=D34=743=N49=COMPLEX_EXLINK50=DBL9991456=EXLINK +_COMPLEX57=COMPLEXGATE97=N52=20060223-11:18:4660=20060223-11:18:46207 +=XEUR55=XEURFDAX0F2006H200=20060340=259=044=58681=L60954=111=Order58: +1:1140693526109=DBL9991421=238=1167=FUT10=096
        When the format is
        Message dump: 8=TEST.4.29=038435=849=TEST56=TEST50=COMPLEXGATE57=DBL9991134=852=2006 +0223-11:19:351=L6096=0.000011=Order58:1:114069352614=0.000017=11193 54210120=031=0.0000000032=0.000037=132710015138=1.000039=854=155=XEURF +DAX0F2006H58=EUREX Error PRICE60=20060223-11:19:34150=8151=1.0000109= +D BL9991463=0167=TEST=200603207=TEST=244=5868.0000000010=101 25 06/02/23 11:20:00:765 Received TEST message Message dump: 8=TEST.4.29=025135=D34=843=N49=TEST50=TEST=TEST57=COMPLEXGATE97=N52=20 +060223-11:19:1060=20060223-11:19:10207=XEUR55=XEURFDAX0F2006H 200=20060340=259=044=5867.51=L60954=111=Order63:1:1140693551109=DBL999 +1421=238=1167=FUT10=166 26 06/02/23 11:20:01:500 Sent TEST Message Message dump:
      > THe problem with that is, that when perl reads my file
      > which was created on windows, it treats the whole file as one line

      that's odd. most MSDOS/Windows files use CR/LF as the end-of-line marker, but it sounds like your file uses just CR...are you sure it's a DOS file and not a Mac file?

      to read MSDOS/Windows text files, you need to set the input record separator to CR/LF rather than newline (actually, this isn't strictly necessary. if you don't do it, you just end up with a CR at the end of each line. chomp will get rid of it)

      i.e.

      $/="\r\n";

      to read Mac text files, set it to just CR:

      $/="\r";

      see the man page for 'perlvar' for more info.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://535133]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (14)
As of 2015-07-07 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls