Delete nth line (when counted from the bottom)

awkperlsed

There are unknown number of lines in a file. How to delete nth line (when counted from the bottom) with one-liner command (you may use more than one if it is necessary) on Unix platform.

Best Answer

To remove for example the 4th line from the bottom using sed:

tac input | sed '4d' | tac

To overwrite the input file:

tmpfile=$(mktemp)
tac input | sed '4d' | tac > "$tmpfile" && mv "$tmpfile" input
Related Question