How to avoid using sed

I want to replace all single quotes in a string with two single quotes using sed. But when the string contains and the sed command does not replace the single quotes that follow after that. please share some thoughts on this.

+5
source share
3 answers

You do not need to avoid anything in the input :

$ echo "123 ' foo & b'ar" | sed "s/'/''/g"
123 '' foo & b''ar

However, in the “replacement” of a part of a team, it s &has a special meaning: it means “compliance”. Therefore, the above command can be rewritten as follows:

$ echo "123 ' foo & b'ar" | sed "s/'/&&/g"
123 '' foo & b''ar

Select it with \, like everything else that needs to be shielded, if necessary:

$ echo "123 ' foo & b'ar" | sed "s/'/'\&'/g"
123 '&' foo & b'&'ar
+13
source

, , , . & \&, .

. 3.1.2 , .

+5

He works for me

bash>echo "'This is a string with 'single quote' & '&'" | sed "s/'/''/g"
''This is a string with ''single quote'' & ''&''
0
source

All Articles