Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Include Delimiter in Split Output

by Saved (Beadle)
on Jul 30, 2009 at 15:50 UTC ( #784650=perlquestion: print w/ replies, xml ) Need Help??
Saved has asked for the wisdom of the Perl Monks concerning the following question:

The Script...
#!/usr/bin/perl #<"SplitFile.pl"> Split File undef $/; $OPTION=$ARGV[0]; $SEARCH=$ARGV[1]; $FLNAME=$ARGV[2]; $pid = open(XFILE, $FLNAME) or die "Couldn't fork: $!\n"; @finfo=split(/(\n\n)/, <XFILE>); $tRecsInfo = @finfo; @MATCHING = (); if ( $OPTION eq "with" ) { @MATCHING = grep /$SEARCH/i, @finfo; } else { @MATCHING = grep !/$SEARCH/i, @finfo; } $tMATCHING = @MATCHING; print "@MATCHING"; #print "\nI read ", scalar(@finfo), " RECORDS\n"; print "\nI read ", $tRecsInfo, " RECORDS\n"; print "I Selected ", $tMATCHING, " RECORDS\n";
The File ...
rrliupau:*:885:999:RRID - Paul Liu:/home/support/rrliupau:/usr/bin/ksh rrliupau ^rr in ID rrliupau RRID in Desc rrliupau Hard Link to Profile rrliupau Hard Link to Standard RRID Profile rrliupau Size 1078 Standard RRID Profile rrliupau RRID MultiSelect -r || exit 1 rrliupau 2974 -r-xr-xr-x 113 root support 1078 Jun 23 +2007 /home/support/rrliupau/.profile rrliupau slogint=Mon Jul 27 19:33:39 2009, lockout=0000000 rrdanilh:*:892:999:RRID - Nataliya Danilchenko:/home/support/rrdanilh: +/usr/bin/ksh rrdanilh ^rr in ID rrdanilh RRID in Desc rrdanilh Hard Link to Profile rrdanilh Hard Link to Standard RRID Profile rrdanilh Size 1078 Standard RRID Profile rrdanilh RRID MultiSelect -r || exit 1 rrdanilh 2974 -r-xr-xr-x 113 root support 1078 Jun 23 +2007 /home/support/rrdanilh/.profile rrdanilh slogint=Tue Jul 21 15:51:07 2009, lockout=0000000 rrtrthki:*:904:999:RRID - Kim Thanh-Tran:/home/support/rrtrthki:/usr/b +in/ksh rrtrthki ^rr in ID rrtrthki RRID in Desc rrtrthki Hard Link to Profile rrtrthki Hard Link to Standard RRID Profile rrtrthki Size 1078 Standard RRID Profile rrtrthki RRID MultiSelect -r || exit 1 rrtrthki 2974 -r-xr-xr-x 113 root support 1078 Jun 23 +2007 /home/support/rrtrthki/.profile rrtrthki slogint=Tue Jul 21 19:58:15 2009, lockout=0000000
I want the output to include the blank line. Thanx much

Comment on Include Delimiter in Split Output
Select or Download Code
Re: Include Delimiter in Split Output
by kennethk (Monsignor) on Jul 30, 2009 at 16:06 UTC
    If you'd like the output of a split to keep the delimiter in the output string, you can use look ahead or look behind assertions (Looking ahead and looking behind), depending on which side you want the delimiter to stick to. For example:

    #!/usr/bin/perl use strict; use warnings; while (<DATA>) { my @line = split/(?<=x)/; print join "\n", @line; } __DATA__ 1234x456x789

    However, this means that one of the records will be inconsistently formatted as compared to the others, which is generally something to be avoided. It usually makes a lot more sense to just append the new lines after the split (perhaps using .=) or include the formatting in your print statement.

      Indeed. If the goal, as stated in the OP, is "output to include the blank line", then you probably shouldn't put it in the element, you should put it in your output--print/say/whatever--because otherwise your last element will look different than the rest when you print them out (and any regex mucking about you do on them may behave differently on the last element).
Re: Include Delimiter in Split Output
by jwkrahn (Monsignor) on Jul 30, 2009 at 18:44 UTC

    Perhaps using paragraph mode.   Something like this:

    #!/usr/bin/perl #<"SplitFile.pl"> Split File use warnings; use strict; my ( $OPTION, $SEARCH, $FLNAME ) = @ARGV; open XFILE, '<', $FLNAME or die "Couldn't open '$FLNAME': $!\n"; local $/ = ''; # Set paragraph mode while ( <XFILE> ) { if ( $OPTION eq 'with' && /$SEARCH/i ) { print; } elsif ( $OPTION ne 'with' && !/$SEARCH/i ) { print; } }
Re: Include Delimiter in Split Output
by Marshall (Prior) on Jul 30, 2009 at 18:52 UTC
    You appear to have record oriented data and want to get a subset of info out of those records...or so it appears to me.. There is another discussion going on about the range operator (/abc/.../xyz/), see: http://www.perlmonks.org/?node_id=783119

    This is a different approach than your splitting on \n\n. And can also process huge files as this is done line by line, record by record without requiring the whole file to be read into a single scalar by redefining the record separator.

    I am still considering some of the issues in this thread myself, but I think you will find it interesting and probably useful.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-12-21 16:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (106 votes), past polls