简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM