Reputation: 9466
I’m trying to write a CLI command to change the contents of a config file.
The config file has these lines:
JAVA_OPTS="$JAVA_OPTS -XX:+UseParNewGC"
JAVA_OPTS="$JAVA_OPTS -XX:+UseConcMarkSweepGC"
I want to change those two lines to this one line:
JAVA_OPTS="$JAVA_OPTS -XX:+UseG1GC"
I want to replace multiple lines, so I’m trying to use Perl to do that.
perl -i -pe 's/JAVA_OPTS="\$JAVA_OPTS -XX:\+UseParNewGC"\nJAVA_OPTS="\$JAVA_OPTS -XX:\+UseConcMarkSweepGC"/JAVA_OPTS="$JAVA_OPTS -XX:+UseG1GC"/' name-of-file.sh
However, this command doesn’t change the file. I’m guessing the regular expression doesn’t match. I don’t know why. I’ve tried several variations of the command and I’ve tried escaping and double-escaping the $
and +
symbols in the match string, but nothing I do makes a difference. Can someone please offer a solution?
I’m using Bash 3.2.57(1) and Perl 5.18.4 on macOS.
Upvotes: 3
Views: 163
Reputation: 785058
Try this perl command line with -0777
and \R
instead of \n
. -0777
enabled slurp mode in perl to match across multiple lines and \R
lets it match all kind of line breaks (including DOS or OSX or Unix line breaks):
perl -0777 -i -pe 's/JAVA_OPTS="\$JAVA_OPTS -XX:\+UseParNewGC"\nJAVA_OPTS="\$JAVA_OPTS -XX:\+UseConcMarkSweepGC"/JAVA_OPTS="\$JAVA_OPTS -XX:+UseG1GC"/' file
Upvotes: 3