On Unix-like operating systems, the tail command reads a file, and outputs the last part of it (the "tail"). The tail command can also monitor data streams and open files, displaying new information as it is written. For example, it's a useful way to monitor the newest events in a system log in real time.
The tail command shows you data from the end of a file. Usually, new data is added to the end of a file, so the tail command is a quick and easy way to see the most recent additions to a file. It can also monitor a file and display each new text entry to that file as they occur.
You can simply:
(head; tail) < file.txt
And if you need to uses pipes for some reason then like this:
cat file.txt | (head; tail)
Note: will print duplicated lines if number of lines in file.txt is smaller than default lines of head + default lines of tail.
ed
is the standard text editor
$ echo -e '1+10,$-10d\n%p' | ed -s file.txt
For a pure stream (e.g. output from a command), you can use 'tee' to fork the stream and send one stream to head and one to tail. This requires using either the '>( list )' feature of bash (+ /dev/fd/N):
( COMMAND | tee /dev/fd/3 | head ) 3> >( tail )
or using /dev/fd/N (or /dev/stderr) plus subshells with complicated 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
(Neither 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;'
(sed -u 10q; echo ...; tail) < file.txt
Just another variation on the (head;tail)
theme, but avoiding the initial buffer fill issue for small files.
head -10 file.txt; tail -10 file.txt
Other than that, you'll need to write your own program / script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With