sed help - delete last 2 lines.

I have been reading through the sed one liners, trying to understand what is happening.

# delete the last 2 lines of a file
sed 'N;$!P;$!D;$d'

The above will delete the last 2 line of a file. I tried analyzing what happens. And I got lost :frowning:

This is what I understood so far from the above.

N; Read the complete file into the pattern-space.
$!P; Print upto last but one line.
$!D; Delete upto the last-but one line :confused:
$d Delete the last line in the pattern-space.

What is wrong in my analysis ? Please explain what the sed script does ?

Thanks,
Vino

Can anyone help me on this ?

Thanks,
Vino

N; Read the next line into the pattern-space.
$!P; Print the first line of the pattern-space, if the current line is not the last line.
$!D; Delete the first line of the pattern-space, if the current line is not the last line.
$d Delete everything in the pattern-space, if the current line is the last line.