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

Hi all, I need to extract a paragraph of text from a file. The text to look for contains double quotes.
e.g. in ksh with sed:
line_of_text="SYSPROC"."TEST" cat <my text file> | sed -e '/./{H;$!d;}' -e "x;/${line_of_text}/!d;"
--> This does not work as the line_of_text contains double quotes and the sed command must also be in double quotes to translate the variable.
How can this be resolved using perl ?
Many thanks for your appreciated help!
Dirk
  • Comment on Extract a paragraph with double quoted text

Replies are listed 'Best First'.
Re: Extract a paragraph with double quoted text
by graff (Chancellor) on Sep 20, 2005 at 23:29 UTC
    If paragraphs in your text file are consistently separated by one or more blank lines, setting $/ to an empty string will allow you to read the file in "paragraph mode" -- i.e. one paragraph at a time:
    open( IN, $file ); { local $/ = ""; while (<IN>) { # $_ contains a paragraph of one or more lines if ( /\"/ ) { # this paragraph contains at least one double-quote ... } } }
Re: Extract a paragraph with double quoted text
by eff_i_g (Curate) on Sep 20, 2005 at 22:33 UTC
    there may be better methods...
    #!/usr/bin/perl use warnings; use strict; while (<DATA>) { print if m/\b.+?".+?\b/s; } __DATA__ Paragraph to ignore. Paragraph with "double quoted text" to get. Another paragraph to ignore.
Re: Extract a paragraph with double quoted text
by eff_i_g (Curate) on Sep 20, 2005 at 22:39 UTC
    this works for paras enclosed in double quotes.
    #!/usr/bin/perl use warnings; use strict; my $data; { local $/; $data = <DATA>; } my @paras = split /(?<!\w)\n+/, $data; print join "\n\n", grep { m/"/ } @paras; __DATA__ Paragraph to ignore. Paragraph with "double quoted text" to get. Another paragraph to ignore. "Fully quoted paragraph."