How to use sed to remove the last n lines of a file How to use sed to remove the last n lines of a file bash bash

How to use sed to remove the last n lines of a file


I don't know about sed, but it can be done with head:

head -n -2 myfile.txt


If hardcoding n is an option, you can use sequential calls to sed. For instance, to delete the last three lines, delete the last one line thrice:

sed '$d' file | sed '$d' | sed '$d'


From the sed one-liners:

# delete the last 10 lines of a filesed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2

Seems to be what you are looing for.