Skip to main content

Ralsina.Me — Roberto Alsina's website

Heads or Tails?

There is a com­mand, head, to take the first piece of a file.

There is a com­mand, tail, to take the last piece of a file.

Tail does a few ex­tra things, be­cause it's use­ful to watch the end of a file that is be­ing up­dat­ed and sim­i­lar things, but they should be pret­ty sim­i­lar pieces of code, sup­port­ing sim­i­lar op­tion­s, right?

Well, no.

  • head sup­­ports a neg­a­­tive num­ber of lines as ar­gu­­men­t, mean­ing "all but the last N lines", and tail does­n't.

  • Same about bytes in­­stead of lines

That means that you can't quite sim­ply get "all but the first two lines of this file".

But don't wor­ry, this is how you do it:

tac file |head --lines=-2 | tac

Correction: tail has what I wanted. I am just a silly guy that doesn't read the man pages completely, you can do tail -n +2 to do it.

On the oth­er hand, it's not ex­plained in the op­tion, and the syn­tax is dif­fer­ent from head­'s, so it's still slight­ly rant-­wor­thy ;-)

Justin / 2006-05-17 04:14:

(read l; read l;while read l;do echo $l;done) < file

works too... :-)

Amand Tihon / 2006-05-17 05:21:

tail -n +3
starts with the third line

And it works for -c too :
tail -c +20 to start at the 20th character.

The most awfull is, it's even stated in the manpage :)

"If the first character of N (the number of bytes or lines) is a `+', print beginning with the Nth item from the start (...)"

Roberto Alsina / 2006-05-17 12:06:

Not on the linux manpage!

-n, --lines=N
output the last N lines, instead of the last 10

And anyway, it's completely different from head's

-n, --lines=[-]N
print the first N lines instead of the first 10; with the leading ‘-’, print all but the last N lines of each file

Roberto Alsina / 2006-05-17 12:07:

This one I got in the mail from Dirk Eddelbuettel:

sed '1,10d' filename

Works, and is simplest yet.

Roberto Alsina / 2006-05-17 12:08:

Oops, sorry, it **is** in the linux manpage... it's just not in the place where the option is described.


Contents © 2000-2024 Roberto Alsina