we have the following configuration file, env.gc.txt

export SERVER_GC_OPTS="-verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:{{hbase_log_dir}}/gc.log-`date +'%Y%m%d%H%M'`"

We try to remove the -`date +'%Y%m%d%H%M'` syntax from the line containing SERVER_GC_OPTS

sed -i '/SERVER_GC_OPTS/ s/-`date +'%Y%m%d%H%M'`//g'


sed '/SERVER_GC_OPTS/ s/-\`date +'%Y%m%d%H%M'\`//g'

but without success.

What is the best right approach to remove the syntax - -`date +'%Y%m%d%H%M'` from the line ?

(Note: a perl one liner is also an option)

Expected output in env.gc.txt

export SERVER_GC_OPTS="-verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:{{hbase_log_dir}}/gc.log"
  • you just change ' with " and use `\` : sed "/SERVER_GC_OPTS/s/-`date +'%Y%m%d%H%M'`//" – Hossein Vatani Aug 14 '18 at 14:52

Or, just look for a backtick, some non-backtick chars, and another backtick to remove any command substitutions in the file:

sed -i '/SERVER_GC_OPTS/ s/-`[^`]\+`//g' file

The equivalent perl would be

perl -i -pe '/SERVER_GC_OPTS/ && s/-\`.+?\`//g' file

The single quote used in the date command is breaking the sed script.

Try this:

sed '/SERVER_GC_OPTS/s/-`date \+.*"$/"/' env.gc.txt

This will remove the string -`date + and everything until the ending double quote.

  • Would be good to use modern syntax $( ... ) instead of backticks – roaima Aug 14 '18 at 18:02

One way out is to use double quotes around sed, like as:

sed   "/SERVER_GC_OPTS/ s/-\`date +'%Y%m%d%H%M'\`//g"

We backslash the backticks since they come alive under double quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.