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

Hi,

I just wonder if you can help me to solve this problem. I
have been working on this problem for a few days now
but I still do not know how do it.
I am trying to convert the following paragraphs
from:

header title-aaab_bbb_ccc
+section1 section2

into:

.header title-aaab_bbb_ccc section1 section2

Basically, I need to combine the two lines into one line
and remove the "plus" sign.
I tried this:
if ( m/^.header\s(.*)\+(\w+)\s(\w+)$/sm ) { print ".header $1 $2 $3\n";<br> }

The pattern occurs only one time in the file. I tried a
combination of the above codes but nothing seems to work.
thank you so much for your help,

Replies are listed 'Best First'.
Re: please help multiple lines parsing
by Abigail-II (Bishop) on Jan 24, 2004 at 00:07 UTC
    So, you have to replace a newline followed by a plus sign with a space? This will do:
    s/\n[+]/ /;

    Abigail

      while (<>) { if ( m/^header/ ) { (my $output = "." . $_ . <>) =~ s/\n[+]/ /; print $output; last; } }
      This will do the thing :)
        header blah blah blah +blah blah blah +blah blah blah

Re: please help multiple lines parsing
by graff (Chancellor) on Jan 24, 2004 at 16:22 UTC
    This is a very common situation when "parsing" text files that contain multi-line "records". In general, there are three basic methods of attack:
    • Read the file one line at a time, and use branching regex logic (  if (/this/) {...} elsif (/that/) {...} ...), and maintain a "state" variable if necessary, to manage what should be done with each line as you read it. Sometimes this can get very tricky, and you'll hear some monks say that they don't like this approach on general principles.
    • If the records are consistently and unambiguously separated by some constant string pattern (e.g. blank lines, lines of "----" or "====", etc), set perl's "input-record-separator" variable ($/) equal to that string (e.g. "\n\n", "----\n" or "====\n", etc); then use the normal  while (<>) operation to go through the file one record at a time.
    • Set $/ = undef, read the entire content of the file as a single scalar string, and use regex matches and/or split() to break it up or play with record contents. (Of course, this may not work when the input file is too big to fit in available memory.)

    Abigail's suggestion would work best using the third approach:

    $/ = undef; $_ = <>; s/(\n)(header)/$1.$2/g; # (updated: made sure not to lose \n) s/\n\+/ /g; # (updated: forgot to escape the "+") print;
    The list above is ranked in terms of "hardest-to-easiest", and your choice of method should be a matter of picking the easiest one that can do what needs to be done.
Re: please help multiple lines parsing
by ysth (Canon) on Jan 25, 2004 at 06:00 UTC
    Good job describing your input and output and showing what you've tried!

    I see a couple of things in your attempt that may be the problem. First, if the line actually starts ".header", you want to start your regex with m/^\.header; otherwise, you'll get false matches on "\npheader", etc., since the . will match any character. Second, your .* may match many lines worth of data. To limit it to the remainder of the .header line, say ([^\n]*\n) instead, or remove the //s flag and say (.*)\n.

    Since both of these changes will cause it to match fewer inputs instead of more, if your problem is that it isn't matching at all, this won't help. Do you have your entire input in $_? If you only read one line at a time, a regex that is supposed to match across lines isn't going to do what you want.

      thanks so much for your help. I now see the light at the
      end of the tunnel :)