user3777747 user3777747 - 1 month ago 7
Bash Question

How to update value of property file using sed command

I have used a variable(NEW) and assigned a required value to which property needs to be updated and then run a sed command for property (x.DB.databaseName) for file named as db.properties .

NEW='K?useUnicode=true&characterEncoding=utf-8'

sed -i 's:^[ \t]*x.DB.databaseName[ \t]*=\([ \t]*.*\)$:x.DB.databaseName = '${NEW}':' db.properties


But it is updating -

x.DB.databaseName = k?useUnicode=x.DB.databaseName=h1amp;characterEncoding=utf-8 where h1 was the previous value assigned to that property.

Required Result-
x.DB.databaseName = K?useUnicode=true&characterEncoding=utf-8

Answer

It is ampersign due to which required output was not coming.

Used '\&' for &

NEW=K?useUnicode=true'\&'characterEncoding=utf-8

then run above sed command.

Gives desired output