Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
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.

Replies are listed 'Best First'.
Re: Extract paragraph from multi line string
by kennethk (Abbot) 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 AnomalousMonk (Chancellor) 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 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 Krambambuli (Curate) 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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2017-06-28 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (640 votes). Check out past polls.