in reply to Slicing the output of a command

Reading files directly in Perl may be more efficient. Otherwise, here is a head demo in Perl for reading output from a binary command and wanting the first count lines. Open may be used to start a command. Note the | after $cmd. Closing the handle stops the command, likely from receiving a PIPE signal.

This is written mainly to showcase a feature of open. Scripting is lots of fun and TIMTOWTDI. Have fun orchestrating all the tools available to you.

use strict; use warnings; sub head { my ($cmd, $count) = (shift, shift || 10); my @output; open my $fh, "$cmd |" or die "error: '$cmd' failed"; while (<$fh>) { push @output, $_; last if $. == $count; } close $fh; return @output; } my @foo = head('cat very_large_file', 4); print @foo;