Insert text N lines before the last line

Using ed:

$ printf '$-1i\nNew line\n.\n,p\n' | ed -s file
1
2
3
New line
4
5

The ed editing script:

$-1i
New line
.
,p

This first moves to the line one line up from the end ($-1) and inserts (i) new contents above that line. The contents inserted is ended with the single dot (it's allowed to be multiple lines). The last ,p displays the complete modified buffer on the terminal.

You may redirect this to a new file, or you may write it back to the original file using

printf '$-1i\nNew line\n.\nw\n' | ed -s file

(the ,p is changed to w).

This latter is also how you would similarly use ex for this job:

printf '$-1i\nNew line\n.\nw\n' | ex -s file

ed and ex are standard line-oriented editors (as opposed to full-screen editors) that should come with your system. Note that -s means different things to each, but is appropriate for both when doing batch mode editing tasks like this.

  • ed. "Shell and utilities". Base specifications. IEEE 1003.1:2017. The Open Group.
  • ex. "Shell and utilities". Base specifications. IEEE 1003.1:2017. The Open Group.

Further reading:

  • Dale Dougherty and Tim O'Reilly (1987). "Advanced Editing". Unix Text Processing. Hayden Books.

It can be done by simple head and tail:

$ output=$(head -n -2 file ; echo 'new line' ; tail -2 file)
$ echo "$output" > file

As mentioned in comments , it will eat any trailing blank lines. So , for preserving trailing blank lines,

$ head -n -2 file >> file.tmp
$ echo 'new line' >> file.tmp
$ tail -2 file >> file.tmp
$ mv file.tmp file

or single liner

$ head -n -2 file >> file.tmp ; echo 'new line' >> file.tmp; tail -2 file >> file.tmp ; mv file.tmp file

You could use GNU sed too:

sed -zE 's/(\n[^\n]*){3}$/\nNew-line&/' infile

Insert a line New-line in third last line of the file (Insert a line two lines before the last line).

This (\n[^\n]*){3}$ matches \newline followed by anything but not a \newline and maximum 3 times from end of the file where -z option casing sed to read a file as a single line (separate lines by NUL characters). So it will match below only (between asterisks I highlighted):

3*\n
4\n
5\n*

Portability, you would use:

sed -e ':t;N;$!bt; s/\(\n[^\n]*\)\{2\}$/\nNew-line&/' infile

if still your sed cannot handle a \newline in replacement part, you can have actual newline or press Ctrl+V followed by Enter which will print ^M control characters of enter key:

sed -e ':t;N;$!bt; s/\(\n[^\n]*\)\{2\}$/\
New-line&/' infile

How it works

The N append each line sed read to the pattern space followed by a new line added until all lines read. The :t defines a label and $!bt telling sed jump to the label called t as long as it's not end of the file.