0

I am using below sed command, but if MacAddressPasswordeRegisteryValue is WElcome12# then its producing WElcome12 and removing #. Any way to avoid it?

sed -i "s#^mac.address.sftp.user.password=.*#mac.address.sftp.user.password=${MacAddressPasswordeRegisteryValue#*=}#" $APP_CONFIG_FILE
Kusalananda
  • 333,661
man
  • 1

1 Answers1

1

Assuming the password may contain any character, then no delimiter that you use for the sed expression is safe to use. If you had, for example, s/.../.../ and the password contained /, you would have the same issue again.

Therefore, don't use sed here. Instead,

awk -v pw="$MacAddressPasswordeRegisteryValue" \
    'BEGIN { OFS=FS="=" }
    $1 == "mac.address.sftp.user.password" { print $1, pw; next } 1' \
    "$APP_CONFIG_FILE" >"$APP_CONFIG_FILE"-new

This would transform

mac.address.sftp.user.password=something old

into

mac.address.sftp.user.password=hello world !#$/

given that $MacAddressPasswordeRegisteryValue was the string hello world !#$/. Other lines would be passed through unmodified to the new file "$APP_CONFIG_FILE"-new.

Kusalananda
  • 333,661