Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sed error "sed: unmatched '/'"

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 /?

like image 760
MOHAMED Avatar asked Jul 31 '14 11:07

MOHAMED


2 Answers

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
like image 64
konsolebox Avatar answered Oct 02 '22 03:10

konsolebox


Check if your $param or $line variables have \n (newline), in my case, that was the reason of the error.

like image 38
fsaravia Avatar answered Oct 02 '22 03:10

fsaravia