Variants of this question have certainly been asked several times in different places, but I am trying to remove the last M
lines from a file without luck.
The second most voted answer in this question recommends doing the following to get rid of the last line in a file:
head -n -1 foo.txt > temp.txt
However, when I try that in OSX & Zsh, I get:
head: illegal line count -- -1
Why is that? How can I remove the M
last lines and the first N
lines of a given file?
Best Answer
You can remove the first 12 lines with:
(That means print from the 13th line.)
Some implementations of
head
like GNUhead
support:but that's not standard.
would work on those systems that have
tail -r
(see also GNUtac
) but is sub-optimal.Where
n
is 1:You can also do:
to remove 2 lines, but that's not optimal.
You can do:
But beware that won't work with large values of n with some
sed
implementations.With
awk
:To remove
m
lines from the beginning andn
from the end: