[tpm] Writing to STDOUT in batches

Richard Dice rdice at pobox.com
Mon Dec 6 07:38:15 PST 2010


Maybe you are trying to complicate this?  I think you've got the pieces you
need right there.  Here's an experiment I just tried.  Note how test2.pl is
the same as test.pl, but with the pipe to sed sucked into the program rather
than built up as a pipeline on the command line...

#!/opt/local/bin/perl

use warnings;
use strict;

foreach my $i ( 0 .. 40 ) {
   print "$i\n";
}

exit 0;
richard-dice-s-computer-9:Desktop rdice$ perl test.pl | sed -n '10,20p'
9
10
11
12
13
14
15
16
17
18
19

richard-dice-s-computer-9:Desktop rdice$ cat test2.pl
#!/opt/local/bin/perl

use warnings;
use strict;

open TEST, "| sed -n '10,20p'";

foreach my $i ( 0 .. 40 ) {
    print TEST "$i\n";
}

close TEST;

exit 0;
richard-dice-s-computer-9:Desktop rdice$ perl test2.pl
9
10
11
12
13
14
15
16
17
18
19



On Mon, Dec 6, 2010 at 10:22 AM, Antonio Sun <antoniosun at lavabit.com> wrote:
> Hi,
> I have the following code in a loop:
>     open(OUTFILE, "$cmd >> myfile"); # append write
>    print OUTFILE content();
>    close(OUTFILE);
> The $cmd is a complicated sed command. For simplicity purpose, let's say
it
> is
>   | sed -n '10,20p'
> I.e., printing only lines 10~20 of the content of each loop.
> My goal is to write to STDOUT instead of a fixed file. I tried to change
the
> above open statement with
>    open(OUTFILE, "$cmd >-");   # write to STDOUT
> or,
>    open(OUTFILE, "$cmd >>-");   # write to STDOUT
> but didn't get any output.
> Anyone can help me here?
> BTW, in case you wonder why the open statement is in the loop -- if I
don't
> do it this way and open outside the loop instead, I'll  get the
> first 10~20 lines only from the first loop.
> Thanks
> antonio
>
>
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20101206/12aeb1f4/attachment.html>


More information about the toronto-pm mailing list