I have a file containing data with the following format:
{"parameter":"toto.tata.titi", "value":"0/2", "notif":"1"}
I make a change on the file with sed:
sed -i "/\<$param\>/s/.*/$line/" myfile
which line
variable is
{"parameter":"toto.tata.titi", "value":"0/2", "notif":"3"}
and param
variable is toto.tata.titi
The above sed
command return error:
sed: unmatched '/'
Because the line
variable is containing /
==> "0/2"
How to update my sed command to make it work even if the line
variable is containing /
?
Your $param
or $line
may contain / on it which causes sed
to have a syntax error. Consider using other delimiters like |
or @
.
Example:
sed -i "/\<$param\>/s|.*|$line|" myfile
But that may not be enough. You can also quote your slashes when they expand:
sed -i "/\<${param//\//\\/}\>/s|.*|$line|" myfile
Other safer characters can be used too:
d=$'\xFF' ## Or d=$(printf '\xFF') which is compatible in many shells.
sed -i "/\<${param//\//\\/}\>/s${d}.*${d}${line}${d}" myfile
Check if your $param
or $line
variables have \n
(newline), in my case, that was the reason of the error.
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