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

Hi Folks Have been looking around and have not seen a satisfactory answer for my problem. Which is as follows:

Say I have a text file which includes the following lines
----------------------
This line has a printf and is
multiline
This line has printf and is one line
This line will not be printed
----------------------

I want to print out every line with "printf" in it, but I want to report the multiline line completely, i.e the output should look like

This line has a printf and is
multiline
This line has printf and is one line

Could anyone suggest a solution?
Thank you

mndoci

"What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'

Replies are listed 'Best First'.
Re: regex across multiple lines
by kvale (Monsignor) on Jun 28, 2002 at 05:34 UTC
    The first question is: what distinguishes a multiline from a single line in your text file? In your example, I see four separate lines, and only my knowledge of English allows me to associate "multiline" with the previous line. Regexes are powerful, but they don't speak English yet :)

    -Mark

      Good question
      and therein lies the problem. I can't find any difference other than the inclusion of printf (which is always on the first line).

      mndoci

      "What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'

        Well, so how can you - as a human being - distinguish whether a line is part of a multiline. If you can't do that, then how should the computer be able to do it?

        -- Hofmator

Re: regex across multiple lines
by frankus (Priest) on Jun 28, 2002 at 09:08 UTC
    $/=undef; $_=<DATA>; s/([^\n]*printf.*?printf.*?\n)[^\n]*\n/$1/gs; print; __DATA__ This line has a printf and is multiline This line has printf and is one line This line will not be printed

    Based upon the test data provided, this works.
    Are we to assume that the multiline is a multiline printf function?,
    if so surely the line ends with a ';'?

    More information please.

    --

    Brother Frankus.

    ¤

      You were quite right. That was the real problem and of course resolved in a similar way to what was suggested. I was curious about the possiblity of solving the problem if I had been provided a file with no ';'. It appears that the question in itself is unsound.

      Regards

      mndoci

      "What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'