OSX sed: Match Bash Variable, and then Delete or Append Bash Variable

Bash: GNU bash, version 4.3.33(1)-release (x86_64-apple-darwin14.1.0)

Working with Neo4j, I wanted to simplify turning on/ off debugging via the Neo4j properties file, so I decided to use sed to do this. This code is just the sed lines to do this.

This has the infamous OS X -i ‘’ argument to do the substitution in place.

# This is the line I either want - to don’t want - in my neo4j-wrapper.conf file:
DEBUG_CONFIG="wrapper.java.additional=-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=y"

sed -i '' "/\(^${DEBUG_CONFIG}$\)"'/d' ./neo4j-wrapper.conf

# This is the line after which I will add my debug config line

# After the match line, append the Debug configuration.
sed -i '' "/\(^${MATCH_LINE}$\)"'/a\'$'\n'"${DEBUG_CONFIG}"  ./neo4j-wrapper.conf

Thanks to jw013’s answer at http://unix.stackexchange.com/a/52141/118244 I was able to sort out the $’\n’ syntax to encourage bash to pass the newline to sed.

See also: http://unix.stackexchange.com/questions/52131/sed-on-osx-insert-at-a-certain-line

Facebook comments: