Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace to apostrophe ' inside a file using SED

Tags:

linux

unix

sed

I have a file "test.txt" that contain the following

+foo+
+bar+

What I want to do is to replace them into:

'foo'
'bar'

But why this code doesn't work?

sed  's/\+/\'/' test.txt

What's the right way to do it?

like image 319
neversaint Avatar asked Aug 08 '13 06:08

neversaint


People also ask

How do you change the apostrophe in sed?

Use " instead. And add g flag to replace all. sed "s/\$/\'/g" ut. txt - this is adding ' at the end instead replacing the $ character.

How do you replace a variable in a file using sed?

How SED Works. In the syntax, you only need to provide a suitable “new string” name that you want to be placed with the “old string”. Of course, the old string name needs to be entered as well. Then, provide the file name in the place of “file_name” from where the old string will be found and replaced.

Which sed command is used for replacement?

The s command (as in substitute) is probably the most important in sed and has a lot of different options. The syntax of the s command is ' s/ regexp / replacement / flags '.


1 Answers

Use " instead. And add g flag to replace all.

sed  "s/\+/\'/g" test.txt
like image 76
falsetru Avatar answered Oct 21 '22 02:10

falsetru