173

Say you have a txt file, what is the command to view the top 10 lines and bottom 10 lines of file simultaneously?

i.e. if the file is 200 lines long, then view lines 1-10 and 190-200 in one go.

3
  • What do you mean "in one go" ?
    – cnicutar
    Commented Dec 24, 2011 at 13:02
  • @cnicutar ie. not going head -10 file looking at the data and then separately going tail -10 file and looking at the data
    – toop
    Commented Dec 24, 2011 at 13:04
  • @toop If you want a real working example, see stackoverflow.com/a/44849814/99834
    – sorin
    Commented Jun 30, 2017 at 15:18

23 Answers 23

271

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.

18
  • 79
    Strictly speaking, this doesn't give you the tail of the original file, but the tail of the stream after head has consumed the first 10 lines of the file. (Compare this with head < file.txt; tail < file.txt on a file with fewer than 20 lines). Just a very minor point to keep in mind. (But still +1.)
    – chepner
    Commented Mar 30, 2012 at 17:20
  • 20
    Nice. If you want a gap between the head and tail parts: (head;echo;tail) < file.txt Commented May 23, 2012 at 11:02
  • 11
    @nametal Actually, you might not even get that much. While head only displays the first 10 lines of its input, there is no guaranteed that it didn't consume more of it in order to find the 10th line ending, leaving less of the input for less to display.
    – chepner
    Commented Feb 12, 2016 at 16:58
  • 44
    Sorry to say, but the answer only works in some cases. seq 100 | (head; tail) gives me only first 10 numbers. Only on much larger input size (like seq 2000) the tail gets some input.
    – modular
    Commented Sep 21, 2016 at 18:57
  • 7
    Note: The buffering issue (head swallows to much from the input stream on small files so that no tail is printed) is fixed by an answer below: cat file.txt | (sed -u 10q ; echo ; tail)
    – haui
    Commented Oct 23, 2019 at 9:53
26

ed is the standard text editor

$ echo -e '1+10,$-10d\n%p' | ed -s file.txt
2
  • 2
    What if the file has more or less than 200 lines? And you don't know the number of lines ab initio?
    – Paul
    Commented Dec 24, 2011 at 13:07
  • @Paul I've changed sed to ed
    – kev
    Commented Dec 24, 2011 at 13:34
26
(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.

1
  • This worked best for me, but for the Mac, I had to brew install gnu-sed to get this to work.
    – steveb
    Commented Jul 18, 2023 at 17:46
22

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;'
3
  • 1
    +1 for stream support. You could reuse stderr: COMMAND | { tee >(head >&2) | tail; } |& other_commands
    – jfs
    Commented Dec 5, 2013 at 0:07
  • 2
    btw, it breaks for files larger than the buffer size (8K on my system). cat >/dev/null fixes it: COMMAND | { tee >(head >&2; cat >/dev/null) | tail; } |& other_commands
    – jfs
    Commented Dec 5, 2013 at 0:47
  • I loved the solution, but after playing for a a while I noticed that in some cases the tail was running before the head ... there are no guaranteed ordering between head and tail commands :\ ...
    – Jan
    Commented Oct 17, 2019 at 5:30
7

Based on J.F. Sebastian's comment:

cat file | { tee >(head >&3; cat >/dev/null) | tail; } 3>&1

This way you can process first line and the rest differently in one pipe, which is useful for working with CSV data:

{ echo N; seq 3;} | { tee >(head -n1 | sed 's/$/*2/' >&3; cat >/dev/null) | tail -n+2 | awk '{print $1*2}'; } 3>&1
N*2
2
4
6
6

It took make a lot of time to end-up with this solution which, seems to be the only one that covered all use cases (so far):

command | tee full.log | stdbuf -i0 -o0 -e0 awk -v offset=${MAX_LINES:-200} \
          '{
               if (NR <= offset) print;
               else {
                   a[NR] = $0;
                   delete a[NR-offset];
                   printf "." > "/dev/stderr"
                   }
           }
           END {
             print "" > "/dev/stderr";
             for(i=NR-offset+1 > offset ? NR-offset+1: offset+1 ;i<=NR;i++)
             { print a[i]}
           }'

Feature list:

  • live output for head (obviously that for tail is not possible)
  • no use of external files
  • progressbar one dot for each line after the MAX_LINES, very useful for long running tasks.
  • progressbar on stderr, assuring that the progress dots are separated from the head+tail (very handy if you want to pipe stdout)
  • avoids possible incorrect logging order due to buffering (stdbuf)
  • avoid duplicating output when total number of lines is smaller than head + tail.
1
  • This is the best answer here. I added it to my .bashrc in a function so I can just pipe to headtail.
    – jamesbtate
    Commented May 20, 2021 at 18:25
5

head -10 file.txt; tail -10 file.txt

Other than that, you'll need to write your own program / script.

7
  • 1
    Nice, I have always used cat and head or tail piped, good to know that I can use them individually!
    – Paul
    Commented Dec 24, 2011 at 13:09
  • How can I then pipe these first 10+last 10 into another command?
    – toop
    Commented Dec 24, 2011 at 13:12
  • 1
    @Paul - with 'your_program' as wc -l it returns 10 instead of 20
    – toop
    Commented Dec 24, 2011 at 13:18
  • 4
    or, without having to spawn a subshell: { head file; tail file; } | prog (spacing inside the braces, and the trailing semicolon are required) Commented Dec 24, 2011 at 19:25
  • 1
    Wow... a down-vote for having an answer quite similar to others (yet timestamped before them) after almost two years, from someone who chose not to post why they down-voted. Nice!
    – mah
    Commented Oct 12, 2013 at 8:17
5

the problem here is that stream-oriented programs don't know the length of the file in advance (because there might not be one, if it's a real stream).

tools like tail buffer the last n lines seen and wait for the end of the stream, then print.

if you want to do this in a single command (and have it work with any offset, and do not repeat lines if they overlap) you'll have to emulate this behaviour I mentioned.

try this awk:

awk -v offset=10 '{ if (NR <= offset) print; else { a[NR] = $0; delete a[NR-offset] } } END { for (i=NR-offset+1; i<=NR; i++) print a[i] }' yourfile
4
  • it needs more work in order to avoid issues when offset is larger than the file
    – Samus_
    Commented Dec 24, 2011 at 13:37
  • Yay, this works with piped output, not just files: a.out | awk -v ... Commented Apr 22, 2013 at 21:07
  • indeed :) but that's awk's normal behavior, most commandline programs work on stdin when invoked without arguments.
    – Samus_
    Commented Apr 24, 2013 at 0:05
  • 1
    Very close to the desired behaviour but it seems that for <10 lines it does add extra new lines.
    – sorin
    Commented Jun 28, 2017 at 10:54
3

Well, you can always chain them together. Like so, head fiename_foo && tail filename_foo. If that is not sufficient, you could write yourself a bash function in your .profile file or any login file that you use:

head_and_tail() {
    head $1 && tail $1
}

And, later invoke it from your shell prompt: head_and_tail filename_foo.

3

I have been looking for this solution for a while. Tried it myself with sed, but the problem with not knowing the length of file/stream beforehand was insurmountable. Of all the options available above, I like Camille Goudeseune's awk solution. He did make a note that his solution left extra blank lines in the output with a sufficiently small data set. Here I provide a modification of his solution that removes the extra lines.

headtail() { awk -v offset="$1" '{ if (NR <= offset) print; else { a[NR] = $0; delete a[NR-offset] } } END { a_count=0; for (i in a) {a_count++}; for (i=NR-a_count+1; i<=NR; i++) print a[i] }' ; }
1

First 10 lines of file.ext, then its last 10 lines:

cat file.ext | head -10 && cat file.ext | tail -10

Last 10 lines of the file, then the first 10:

cat file.ext | tail -10 && cat file.ext | head -10

You can then pipe the output elsewhere too:

(cat file.ext | head -10 && cat file.ext | tail -10 ) | your_program

3
  • 6
    Why use cat when you can just call head -10 file.txt?
    – jstarek
    Commented Dec 24, 2011 at 14:52
  • Can you make the number of lines variable, so the call is something like: head_ tail(foo, m,n) - returning the first m snd last n lines of text?
    – ricardo
    Commented Jul 22, 2012 at 20:02
  • @ricardo that would involve writing a bash script that takes 3 args and passes them to tail and head or a function by alias-ing it.
    – Paul
    Commented Jul 23, 2012 at 10:19
1

I wrote a simple python app to do this: https://gist.github.com/garyvdm/9970522

It handles pipes (streams) as well as files.

2
  • 2
    Would be best to post the relevant parts of the code.
    – fedorqui
    Commented Oct 21, 2014 at 16:10
  • Basically, it uses collections.deque to simulate tail
    – Mat M
    Commented Apr 8, 2021 at 21:04
1

drawing on ideas above (tested bash & zsh)

but using an alias 'hat' Head and Tails

alias hat='(head -5 && echo "^^^------vvv" && tail -5) < '


hat large.sql
1

Consumes stdin, but simple and works for 99% of use cases

head_and_tail

#!/usr/bin/env bash
COUNT=${1:-10}
IT=$(cat /dev/stdin)
echo "$IT" | head -n$COUNT
echo "..."
echo "$IT" | tail -n$COUNT

example

$ seq 100 | head_and_tail 4
1
2
3
4
...
97
98
99
100
1
0

Why not to use sed for this task?

sed -n -e 1,+9p -e 190,+9p textfile.txt

1
  • 3
    This works for files of known length, but not files whose length is unknown.
    – Kevin
    Commented Nov 13, 2012 at 2:39
0

To handle pipes (streams) as well as files, add this to your .bashrc or .profile file:

headtail() { awk -v offset="$1" '{ if (NR <= offset) print; else { a[NR] = $0; delete a[NR-offset] } } END { for (i=NR-offset+1; i<=NR; i++) print a[i] }' ; }

Then you can not only

headtail 10 < file.txt

but also

a.out | headtail 10

(This still appends spurious blank lines when 10 exceeds the input's length, unlike plain old a.out | (head; tail). Thank you, previous answerers.)

Note: headtail 10, not headtail -10.

0

Building on what @Samus_ explained here about how @Aleksandra Zalcman's command works, this variation is handy when you can't quickly spot where the tail begins without counting lines.

{ head; echo "####################\n...\n####################"; tail; } < file.txt

Or if you start working with something other than 20 lines, a line count might even help.

{ head -n 18; tail -n 14; } < file.txt | cat -n
0

To print the first 10 and last 10 lines of a file, you could try this:

cat <(head -n10 file.txt) <(tail -n10 file.txt) | less

0
sed -n "1,10p; $(( $(wc -l ${aFile} | grep -oE "^[[:digit:]]+")-9 )),\$p" "${aFile}"

NOTE: The aFile variable contains the file's full path.

0

I would say that depending upon the size of the file, actively reading in its contents may not be desirable. In that circumstance, I think some simple shell scripting should suffice.

Here's how I recently handled this for a number of very large CSV files that I was analyzing:

$ for file in *.csv; do echo "### ${file}" && head ${file} && echo ... && tail ${file} && echo; done

This prints out the first 10 lines and the last 10 lines of each file, while also printing out the filename and some ellipsis before and after.

For a single large file, you could simply run the following for the same effect:

$ head somefile.csv && echo ... && tail somefile.csv
0

I did some more experimenting based mostly on suggestions here. After working a bit, I up with something very similar to another version in another comment, but with a focus more on formatting with multiple file args in addition to stdin.

This wraps nicely into a script (tentatively headtail) and uses the gnu awk. On macOs this can be installed via brew install gawk.

It can work on piped content OR a list of files as arguments. Given files, it prints a header of the file name, the head N lines, a skipped lines maker, then the tail N lines. If the head and tail overlap or would line up, it neither includes a skip marker nor does it display any duplicate lines.

#!/bin/bash
headtail_awk() {
  N=10
  gawk -v "n=${N}" -- '\
  FNR == 1 && FILENAME != "-" {
    printf "\033[036m==> %s <==\033[0m\n", FILENAME;
  }
  # print head lines
  FNR <= n { print }
  # store lines in a circular buffer
  { a[FNR % n]=$0 }
  # print non-overlapping tail lines from circular buffer.
  ENDFILE {
    if ( FNR > 2 * n ) {
      printf "\033[0;36m>>> %s lines skipped <<<\033[0m\n", FNR - 2 * n
    }
    for (i=FNR-n+1;i<=FNR;i++) {
      if ( i > n) {
        print a[i % n]
      }
    }
  }
' "$@"
}
headtail_awk "$@"

I'll leave any getopts and/or enhancements of the N=10 line window as an exercise for the reader.

sample output of multiple files (with n=3):

$ headtail -n 3 /usr/share/dict/words /usr/share/dict/propernames
==> /usr/share/dict/words <==
A
a
aa
>>> 235880 lines skipped <<<
zythum
Zyzomys
Zyzzogeton
==> /usr/share/dict/propernames <==
Aaron
Adam
Adlai
>>> 1302 lines skipped <<<
Wolfgang
Woody
Yvonne
0

this worked for me: (head -100) < source.txt > target.txt

(head -100) < source.txt takes the first 100 lines from the source.txt file and then

taget.txt pushes the 100 lines into a new file called target.txt

Initially I thought something like this should work: head -100 source.txt > target.txt but it returned an empty file.

1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Feb 18, 2022 at 10:48
0

In order to get the first x and last y lines of of output of a command or pipeline, I found it best to use several shell features:

(head -n $x; tail -n $y) <<< "$(your_pipeline_here)"

(list) is a compound command to run list in a subshell, <<< introduces a here document, $() is command substitution, and "" keeps the line breaks in the pipeline's output.

If you want, you can add echo; in between the head and tail commands to print a blank line (or a non-blank line if you give echo one or more arguments). Note that -n $x can be shorted to -$x if $x expands to a positive integer with no leading +. Example:

(head -3; echo; tail -4) <<< "$(seq 10)"

Output:

1
2
3

7
8
9
10

Of course, this solution works best if the sum of x and y is <= the number of lines in the command or pipeline's output. It has the advantage of being able to specify arbitrary line limits for head and tail, but the disadvantage is that tail won't repeat any lines that head already printed. Given the use of subshells, it might not perform very well if executed many times. This tradeoff will be worth it for some use cases, but not others.

1
  • It doesn't work for me. It only "heads". Using Bash 6.1.
    – LatinSuD
    Commented Aug 13 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.