sed
is the correct way of doing this, even if you said that you for some reason don't want to use sed
.
The sed script would look like
$i\
this is test4\
this is test5
and you would run that as sed -f script.sed file
. The i
command inserts lines before the addressed line, and the $
addresses the last line of the file.
As a "one-liner" using GNU sed
:
$ sed -e '$i\' -e ' this is test4\' -e ' this is test5' file
{
This is test1
this is test2
this is test3
this is test4
this is test5
}
Depending on whether the file is actually a JSON file or in some other structured text format, there may be tools like jq
that are better suited for manipulating it.
To use echo
as you requested (this also assumes that you are using head
from GNU coreutils as the -n
option does not usually take a negative number):
{ head -n -1 file
echo ' this is test4'
echo ' this is test5'
tail -n 1 file; } >newfile