Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Extract paragraph from multi line string

by yanirdz (Initiate)
on Jan 26, 2010 at 15:24 UTC ( #819695=perlquestion: print w/ replies, xml ) Need Help??
yanirdz has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have trouble extracting paragraph from multi line string
# the UNIX tool output is a multi line text my $var = `unix_tool file_name`; open(FH,'<', \$var) or die "ERROR\n"; $/ = ""; #This is just for example while (<FH>){ print "------------------------------------------\n"; print "$_"; print "\n"; }
Nothing gets printed. If I donít use:
$/ = "";
All the lines get printed. And one more thing, If I first save the UNIX tool output file and open it using file handler the code read every paragraph.

Comment on Extract paragraph from multi line string
Select or Download Code
Re: Extract paragraph from multi line string
by kennethk (Monsignor) on Jan 26, 2010 at 15:37 UTC
    Once I swap my $var = `unix_tool file_name`; to my $var = `cat script.pl`;, I get the expected output. What is unix_tool?

    You should be able to split this into two steps (reading in from the backticks followed by the angle-bracket read). What happens when you copy the tool output into a DATA block and use that for your read, i.e.:

    #!/usr/bin/perl use strict; use warnings; $/ = ""; #This is just for example while (<DATA>){ print "------------------------------------------\n"; print "$_"; print "\n"; } __DATA__ This is the output From the tool.

    Assuming this fails, post the modified snippet (scrubbed if necessary) and we can work from there.

Re: Extract paragraph from multi line string
by jwkrahn (Monsignor) on Jan 26, 2010 at 15:58 UTC
    ( my $var = `unix_tool file_name` ) =~ s/(.+?)\n{2,}/----------------- +-------------------------\n$1\n/sg; print $var;
Re: Extract paragraph from multi line string
by ikegami (Pope) on Jan 26, 2010 at 16:15 UTC
    If it prints nothing, you got nothing. Check for errors.
Re: Extract paragraph from multi line string
by AnomalousMonk (Abbot) on Jan 26, 2010 at 16:24 UTC

    One 'problem' with reading input in 'paragrep' mode (i.e.,  $/ = "") is that a seemingly blank line that has any other whitespace in it (e.g.,  "\n \n") is not recognized as being a repeated newline. A repeated newline, and therefor a paragraph break, exactly matches  /\n\n+/ only.

    BTW: I get the same printed output from  `cat file` as kennethk.

Re: Extract paragraph from multi line string
by Krambambuli (Deacon) on Jan 26, 2010 at 17:45 UTC
    Rephrasing what ikegami said already:

    the output of `unix_tool file_name` is probably _empty_.

    What if you run this very command in the shell?
    What if you use the full path to unix_tool inside the backticks, if unix_tool isn't in the current directory?

    Maybe these questions can help you in tracking the problem down.

    Update: what if you dump out $var after filling it, like

    use Data::Dumper;
    print Dumper( $var );

    Altough this sounds improbable, maybe you just falled over some bug, maybe one that appears only within your environment: what Perl do you use, on what OS?


    Krambambuli
    ---

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://819695]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-12-18 07:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (44 votes), past polls