7

Suppose my file a.conf is as following

Include /1
Include /2
Include /3

I want to replace "Include /2" with a new line, I write the code in .sh file :

line="Include /2"
rep=""
sed -e "s/${line}/${rep}/g" /root/new_scripts/a.conf

But after running the sh file, It give me the following error

sed: -e expression #1, char 14: unknown option to `s' 
3
  • 3
    By editing your question to substantively change what you're asking, it is very hard to vote meaningfully on answers; indeed, the answer you accepted no longer addresses the question. Perhaps you should rollback and post a new question. Commented Dec 13, 2011 at 10:49
  • I thought that was my answer, but when a new criteria came, then I was bemused, sorry for that. Commented Dec 13, 2011 at 12:13
  • I would like to point out when you say "I want to replace "Include /2" with a new line you are in fact replacing the string with nothing. It's sed that is providing the newline once you have finished your editing. Commented Dec 13, 2011 at 12:51

4 Answers 4

21

If you are using a newer version of sed you can use -i to read from and write to the same file. Using -i you can specify a file extension so a backup will be made, incase something went wrong. Also you don't need to use the -e flag unless you are using multiple commands

sed -i.bak "s/${line}/${rep}/g" /root/new_scripts/a.conf

I have just noticed that as the variables you are using are quoted strings you may want to use single quotes around your sed expression. Also your string contains a forward slash, to avoid any errors you can use a different delimiter in your sed command (the delimiter doesn't need to be a slash):

sed -i.bak 's|${line}|${rep}|g' /root/new_scripts/a.conf
Sign up to request clarification or add additional context in comments.

3 Comments

I'm using shell script, and single quotes do not give the variable replacement necessary. Double quotes replaces ${rep} with the proper value.
This answer does not work (because of the slash character in the text). so no answer for this ?
Cheers for the pointer about being able to use a delimited other than /
4

You have to write the changes to a new file and then, move the new file over the old one. Like this:

line="Include 2"

rep=""

sed -e "s/${line}/${rep}/g" /root/new_scripts/a.conf > /root/new_scripts/a.conf-new

mv /root/new_scripts/a.conf-new /root/new_scripts/a.conf

Comments

4

The redirection (> /root/new_scripts/a.conf) wipes the contents of the file before sed can see it.

You need to pass the -i option to sed to edit the file in-place:

sed -i "s/${line}/${rep}/g" /root/new_scripts/a.conf

You can also ask sed to create a backup of the original file:

sed -i.bak "s/${line}/${rep}/g" /root/new_scripts/a.conf

2 Comments

Hey, your answer is correct but I have a new problem now. Look at my question again.
Please dont mind, if this question is mark solved then nobody answered this.
1

So, if you have to replace a substring in a file, you can use sed command like this, say we have a file as file.txt, so replacing a substring in it can be done like this

searchString="abc";
replaceString="def";
sed -i '' "s|$searchString|$replaceString|g" file.txt

This will all the occurrences of "abc" with "def" in file.txt. Also, this keeps a check for any / character present in the variables used, and with no backup file made.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.