sed to replace line which contains space wildcards

I have file which has 1500 line and I want to change line number 1449 I know I can use below command

sed -i '1449s/"'$' == $rdn{ strlen($rdn) - 1 })"/'$' == $rdn[ strlen($rdn) - 1 ])/' file_name

but it’s not working. I want to replace

'$' == $rdn{ strlen($rdn) - 1 })  


'$' == $rdn[ strlen($rdn) - 1 ])

Anyone knows how to replace this with specifying line number in sed, please guide.

Asked By: Samurai


Your problem is not the line number nor sed, it’s the quoting: You can’t nest double quotes inside single quotes, so '1449s/"' has matching single quotes, thus your script starts with 1449s/"$ == and so on.

Your pattern is probably unique without the '$', so you could work with single quotes only:

sed -i '1449s/ == $rdn{ strlen($rdn) - 1 })/ == $rdn[ strlen($rdn) - 1 ])/' file_name

Or you put the whole thing inside double quotes, but need to escape the $ from your shell:

sed -i "1449s/'$' == $rdn{ strlen($rdn) - 1 })/'$' == $rdn[ strlen($rdn) - 1 ])/" file_name

Or finally, if the line doesn’t contain any { or } except the ones to be replaced, you can use the y command:

sed -i '1449y/{}/[]/' file_name
Answered By: Philippos
sed  -i -e "1449s/})$/])/g" -e "1449s/{ strlen/[ strlen/g" filename


'$' == $rdn[ strlen($rdn) - 1 ])
Answered By: Praveen Kumar BS
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.