0

One liner in bash (for example using sed) to append a line before the last line in a file.

Example

Old File

foo bar
foo bar
foo bar
foo bar

New file

foo bar
foo bar
foo bar
--new foo bar
foo bar

I tried sed on OSX like that

sed '$ i --new foo bar' file.sh

I get this error command i expects \ followed by text. Any ideas?

0

2 Answers 2

1

While some versions of sed (e.g. GNU sed) support the i command on the same line as the text to be inserted, the POSIX standard specifies the usage like this:

sed '$i\
--new foo bar' file.sh

That is, the i is followed by a backslash and a newline, then the text to be inserted.

Sign up to request clarification or add additional context in comments.

1 Comment

so this works as expected :) but I have to add line break after the inserted one. Thanks
1

Backslash and newline or new -e expression is missing after i:

sed -e '$i\' -e '--new foo bar'

1 Comment

@OdinRW: It depends on your version and flavour of sed.