I am trying to replace properties on a properties file using sed in a shell script; the command listed below works perfectly fine
sed "s!${KEY}=.*!${KEY}=${NEWVAL}!" infile > outfile
Issue – this guy replaces the matched “KEY” in comments as well.
example file:
###########
#ws.clients=http://abc123.com
ws.clients=http://123.com
###########
script:
#!/bin/ksh
KEY="ws.clients"
NEWVAL="http://abcd.com"
sed "s!${KEY}=.*!${KEY}=${NEWVAL}!" infile > outfile
output:
###########
#ws.clients=http://abcd.com
ws.clients=http://abcd.com
###########
I tried few ways but wasnt successful in escaping the line starting with “#”… suggestions ?
You can add a condition to the replace expression:
/^[^#]/reads as “everything but a pound sign at the start of the line”.