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

All

I have the following in a text file, and i want to extract
everything between Message dump: and Messsage dump:

file:

Message dump: 1=4 11:13:2006 Message dump: 1=445=3=56=23=67=23=123=12=34 11:13:2006 Message dump:

How would one go about that, i did try /Message dump:(.*)Message dump:/sm but that does not go over more than 1 line

Replies are listed 'Best First'.
Re: matching everything between over two lines
by GrandFather (Saint) on Feb 23, 2006 at 09:39 UTC

    Here's one way:

    use warnings; use strict; local $/ = 'Message dump:'; while (<DATA>) { chomp; print; } __DATA__ Message dump: 1=4 11:13:2006 Message dump: 1=445=3=56=23=67=23=123=12=34 11:13:2006 Message dump:

    Prints:

    1=4 11:13:2006 1=445=3=56=23=67=23=123=12=34 11:13:2006

    Update - that loop was a little verbose. It crunches down to:

    chomp, print while <DATA>;

    DWIM is Perl's answer to Gödel
Re: matching everything between over two lines
by prasadbabu (Prior) on Feb 23, 2006 at 09:41 UTC

    Hi minixman, here is one way to do it.

    use strict; local $/; my $str = <DATA>; while ($str =~ /Message dump:((?:(?!Message dump:).)*)/gs) { print "$1"; } __DATA__ Message dump: 1=4 11:13:2006 Message dump: 1=445=3=56=23=67=23=123=12=34 11:13:2006 Message dump: output: 1=4 11:13:2006 1=445=3=56=23=67=23=123=12=34 11:13:2006

    Prasad

      I gave that a go with the following, as i am passing the file in from the command line.
      open(FG,"$ARGV[0]") || die &writelog(1,"INFO: processfile_rt: Unable t +o open log file: $!"); my $str = <FG>; while($str =~ /Message dump:((?:(?!Message dump:).)*)/gs) { print "$1\n"; }
      But that does not seem to print out anything.

      file is
      Message dump: 460 05/12/21 11:41:18:000 Sent FIX Message Message dump: 461 05/12/21 11:41:23:593 Received FIX message Message dump: 462 05/12/21 11:41:48:093 Sent FIX Message Message dump: 463 05/12/21 11:41:55:625 Received FIX message Message dump: 464 05/12/21 11:42:18:015 Sent FIX Message Message dump: 465 05/12/21 11:42:27:531 Received FIX message Message dump:

        You are reading the file line by line, but try matching across lines in your regular expression. That can't work. Either read the whole file into memory and then try the while loop or set the Record Separator $/ to Message dump:, or use the .. operator:

        while (<>) { print if ! /^Message dump:$/) };
        your code appears to only read the first line of the file. Try my $str=join("",<FG>); instead of the second line.
      Sorry i was missing the local $/ read the file at once, i think it works now
Re: matching everything between over two lines
by kwaping (Priest) on Feb 23, 2006 at 15:51 UTC