I want to remove some n lines from the end of a file. Can this be done using sed?
For example, to remove lines from 2 to 4, I can use
$ sed '2,4d' file
But I don't know the line numbers. I can delete the last line using
$sed $d file
but I want to know the way to remove n lines from the end. Please let me know how to do that using sed or some other method.
Try the following command:
I prefer this solution;
where N is the number of lines to remove.
I don't know about
sed
, but it can be done withhead
:With the answers here you'd have already learnt that sed is not the best tool for this application.
However I do think there is a way to do this in using sed; the idea is to append N lines to hold space untill you are able read without hitting EOF. When EOF is hit, print the contents of hold space and quit.
The sed command above will omit last 5 lines.
Most of the above answers seem to require GNU commands/extensions:
For a slightly more portible solution:
OR
OR
Where "10" is "n".
posix version