For a clean stream (for example, output from a command), you can use "tee" to fork a stream and send one stream to the head and from one to the tail. To do this, use the function "> (list)" bash (+ / dev / fd / N):
( COMMAND | tee /dev/fd/3 | head ) 3> >( tail )
or using / dev / fd / N (or / dev / stderr) plus subshells with complex redirection:
( ( seq 1 100 | tee /dev/fd/2 | head 1>&3 ) 2>&1 | tail ) 3>&1 ( ( seq 1 100 | tee /dev/stderr | head 1>&3 ) 2>&1 | tail ) 3>&1
(None of these will work in csh or tcsh.)
For something with a little better control, you can use this perl command:
COMMAND | perl -e 'my $size = 10; my @buf = (); while (<>) { print if $. <= $size; push(@buf, $_); if ( @buf > $size ) { shift(@buf); } } print "------\n"; print @buf;'
RantingNerd Dec 04 '13 at 23:34 2013-12-04 23:34
source share