in reply to Multi-line regex help needed

vxp,
As I indicated in the CB, this limited data sample appears to be a perfect fit for changing $/ into paragraph mode (see perlvar;
{ local $/ = ""; while (<$fh>) { # $_ now has exactly 1 section in it } }

Cheers - L~R

Replies are listed 'Best First'.
Re^2: Multi-line regex help needed
by vxp (Pilgrim) on Jun 18, 2009 at 15:44 UTC

    Thanks, tried that by doing the following:

    #!/usr/bin/perl $file = shift; open(LOG, $file) or die "Couldn't open $file: $!\n"; local $/ = ""; while (<LOG>) { print "================BLAH================\n$_\n"; } close(LOG);

    it didn't quite do what's expected though. $_ doesn't have exactly one section in it. here's what's in it:

    ================BLAH================ abc1-wl-wlc1.domain.com ================BLAH================ Successful downloads: 0 Failed downloads: 1 Warnings: 0 ================BLAH================ Detailed Log Info ================BLAH================ General Expect Errors: 1 Text Dump Failure: 1 Binary Transfer Successful: 1 Attempts to get Text Dump: 1 Attempts to use TFTP: 1 Attempted binary transfers: 1 ================BLAH================ Textual log of the file transfer: Processing abc1-wl-wlc1.domain.com Performing [/usr/bin/ssh -2 -a -o " +StrictHostKeyChecking no" abc1-wl-wlc1.domain.com] Exception: 3:Child + PID 20854 exited with status 256 at /usr/SD/perl-5.6.1/lib/site_perl /EMAN/Config/DeviceConfig/WLC.pm line 288, line 62. ExpectLog: Sorry, +telnet is not allowed on this port!Connection to abc1-wl-wlc1.domain. +com closed. Performing TFTP using SNMP: DestHost [x.x.x.x] Com munity [private] agentTransferUploadServerIP [x.x.x.x] agentTransferUp +loadFilename [x.x.x.x.UCFM.18433] Exception: 3:Child PID 20854 exited + with status 256 at /usr/SD/perl-5.6.1/lib/site_perl/ EMAN/Config/DeviceConfig/WLC.pm line 288, line 62. Updating CVS ================BLAH================ abc1-wl-wlc2.domain.com
      vxp,
      Well then your real data and your posted data are not the same thing. What I provided you would have worked otherwise. Apparently all of your $ are actually not there in your real data.

      At this point, I would use the flip flop operator or my own flag to tell me the start and end of a section. It doesn't make sense to me to use a multi-line regex unless you intend to slurp the entire file into memory (scalability issues) or you intend to create your own sliding buffer window (code complexity issues).

      Cheers - L~R

        I have to respectfully disagree there, what I posted and the "actual data" are one and the same (to the best of my knowledge)

        The full text file (redacted to get rid of an actual domain and IPs) is over here: http://www.yoda.im/pmonks.txt

      now I tried changing $/ to "domain.com" and it's a little closer. Actually _almost_ there:

      #!/usr/bin/perl $file = shift; open(LOG, $file) or die "Couldn't open $file: $!\n"; local $/ = "cisco.com\n"; while (<LOG>) { print "================BLAH================\n$_\n"; } close(LOG);

      Resulting output:

      abc1-wl-wlc1.domain.com ================BLAH================ Successful downloads: 0 Failed downloads: 1 Warnings: 0 Detailed Log Info General Expect Errors: 1 Text Dump Failure: 1 Binary Transfer Successful: 1 Attempts to get Text Dump: 1 Attempts to use TFTP: 1 Attempted binary transfers: 1 Textual log of the file transfer: Processing abc1-wl-wlc1.domain.com Performing [/usr/bin/ssh -2 -a -o " +StrictHostKeyChecking no" abc1-wl-wlc1.domain.com] Exception: 3:Child + PID 20854 exited with status 256 at /usr/SD/perl-5.6.1/lib/site_perl /EMAN/Config/DeviceConfig/WLC.pm line 288, line 62. ExpectLog: Sorry, +telnet is not allowed on this port!Connection to abc1-wl-wlc1.domain. +com closed. Performing TFTP using SNMP: DestHost [x.x.x.x] Com munity [private] agentTransferUploadServerIP [x.x.x.x] agentTransferUp +loadFilename [x.x.x.x.UCFM.18433] Exception: 3:Child PID 20854 exited + with status 256 at /usr/SD/perl-5.6.1/lib/site_perl/ EMAN/Config/DeviceConfig/WLC.pm line 288, line 62. Updating CVS abc1-wl-wlc2.domain.com ================BLAH================ Successful downloads: 0 Failed downloads: 1 Warnings: 0 Detailed Log Info General Expect Errors: 1 Text Dump Failure: 1 Binary Transfer Successful: 1 Attempts to get Text Dump: 1 Attempts to use TFTP: 1 Attempted binary transfers: 1 Textual log of the file transfer: Processing abc1-wl-wlc2.cisco.com Performing [/usr/bin/ssh -2 -a -o "S +trictHostKeyChecking no" abc1-wl-wlc2.domain.com] Exception: 3:Child +PID 20855 exited with status 256 at /usr/SD/perl-5.6.1/lib/site_perl /EMAN/Config/DeviceConfig/WLC.pm line 288, line 58. ExpectLog: Sorry, +telnet is not allowed on this port!Connection to abc1-wl-wlc2.domain. +com closed. Performing TFTP using SNMP: DestHost [x.x.x.x] Com munity [tasMANia] agentTransferUploadServerIP [x.x.x.x] agentTransferU +ploadFilename [x.x.x.x.UCFM.18441] Exception: 3:Child PID 20855 exite +d with status 256 at /usr/SD/perl-5.6.1/lib/site_perl/ EMAN/Config/DeviceConfig/WLC.pm line 288, line 58. Updating CVS
      The correct hostname needs to be in between the blah's - right now BLAH comes right after the correct hostname for the section :/