The title says all. i need to replace a property value whom i don't know to a different value. i'm trying this:
#!/bin/bash
sed -i "s/myprop=[^ ]*/myprop=$newvalue/g" file.properties
i get sed: -e expression #1, char 19: unknown option to
s'`
I think the problem is that $newvalue
is a string that represents a directory so it messes up sed.
What can I do ?
If your property file is delimited with =
sign like this -
param1=value1
param2=value2
param3=value3
then you can use awk
do modifiy the param value by just knowing the param name. For example, if we want to modify the param2
in your property file, we can do the following -
awk -F"=" '/^param2/{$2="new value";print;next}1' filename > newfile
Now, the above one-liner
requires you to hard code the new value of param. This might not be the case if you are using it in a shell script and need to get the new value from a variable.
In that case, you can do the following -
awk -F"=" -v newval="$var" '/^param2/{$2=newval;print;next}1' filename > newfile
In this we create an awk
variable newval
and initialize it with your script variable ($var) which contains the new parameter value.
sed
can use characters other than /
as the delimiter, even though /
is the most common. When dealing with things like pathnames, it's often helpful to use something like pipe (|
) instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With