0

I have written a very simple script which takes a string as input and replace the input text in a file via sed. But I get an error always

sed: -e expression #1, char 40: unknown option to `s'

Shell script is as below

sed -i "s/c9/$1/g" Test.java 

and to run

./code.sh "lmc.driver.findElement(By.xpath(\"//*[@id=\\\"login-email\\\"]\")).clear();\n                        lmc.driver.findElement(By.xpath(\"//*[@id=\\\"login-email\\\"]\")).sendKeys(username);\n                        lmc.driver.findElement(By.xpath(\"//*[@id=\\\"login-password\\\"]\")).clear();\n                        lmc.driver.findElement(By.xpath(\"//*[@id=\\\"login-password\\\"]\")).sendKeys(password);\n                        lmc.driver.findElement(By.xpath(\"//*[@id=\\\"loginForm\\\"]/div[3]\")).click();\n                        lmc.driver.findElement(By.xpath(\"//*[@id=\\\"login-identity-domain\\\"]\")).clear();\n                        lmc.driver.findElement(By.xpath(\"//*[@id=\\\"login-identity-domain\\\"]\")).sendKeys(tenantId);\n                        
lmc.driver.findElement(By.xpath(\"//*[@id=\\\"loginBtn\\\"]\")).click();"

marked as duplicate by don_crissti, Michael Homer, Timothy Martin, Jeff Schaller, G-Man May 24 '17 at 4:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

The string contains unescaped //. After inserting the string the first one will close the substitution, the second will be the unknown option.

You either have to escape the slashes with backslashes to sed (which need to be escaped to the shell, so you need double-backslashes, resulting in ugly\\/\\/ or better use a different delimiter to the s command which is not part of the string like

sed -i "s_c9_$1_g" Test.java

This way the slashes lose their special meaning.

Not the answer you're looking for? Browse other questions tagged or ask your own question.