To remove the last two lines, you can use something like this:
head -n $(($(wc -l < "$filename") - 2)) "$filename"
Not very elegant, but it should work. I use wc -l to count the number of lines, and then use the head to display only the first number of lines minus two lines.
EDIT: N ,
tail -n $N $filename
N , .
head -n $N $filename
, , , , .