Sofiia
Sofiia

Reputation: 3

Adding a new line to the end of a file using sed

I need to add a new line to the end of a file. But when I am running sed command on MacOS I get an error.

The command I run sed -i'' -e '$a\wrapper{gradleVersion='5.5'}' build.gradle

The error I get: sed: 1: "$a\wrapper{gradleVersio ...": extra characters after \ at the end of a command

Upvotes: 0

Views: 151

Answers (1)

Corentin Limier
Corentin Limier

Reputation: 5006

Simplest answer : do not use sed

echo "wrapper{gradleVersion='5.5'}" >> build.gradle

Pretty straightforward right ?

If you want to ignore my advices and keep using sed, to complete @shellter comment :

  • On MacOS sed, you need to add newlines before the text you want to add when using a command.

  • MacOS sed will not add a newline by default after the text you want to add, and because you want the file remains POSIX standard, you'll need to add it

You can do :

sed -i '' -e '$a\
<text to add>
' <file>
  • Because you want to add single quotes, you need to wrap sed command with double quotes, then escape $ and \ which make it particularly annoying to use.

Try this :

sed -i '' "\$a\\
wrapper{gradleVersion='5.5'}
" build.gradle

On gnu-sed (install it with brew brew install gnu-sed), you would do :

gsed -i "\$a wrapper{gradleVersion='5.5'}" build.gradle

Upvotes: 3

Related Questions