Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace ' with sed?

Tags:

bash

sed

I try to use sed within a bash script in order to replace ' with ''
I have to do so because of an Oracle DB, but I cannot find the right syntax.

So far I have tried:

sed -e 's/(')/('')/g' List_employees_a.csv > List_employees.csv
sed -e 's/'/''/g' List_employees_a.csv > List_employees.csv
sed -e 's/'/\'\'/g' List_employees_a.csv > List_employees.csv
sed -e 's/\'/\''/g' List_employees_a.csv > List_employees.csv
like image 967
oz380 Avatar asked Jul 07 '26 14:07

oz380


2 Answers

This sed should work:

sed 's/'\''/'\'''\''/g'

OR use double quoted for delimiters:

sed "s/'/''/g"

OR more verbose:

sq="'"
dq="''"
sed "s/$sq/$dq/g" file
like image 167
anubhava Avatar answered Jul 09 '26 04:07

anubhava


The problem isn't sed, it's that your shell is parsing the quotes before they ever get to sed. One really easy way to avoid this is to use a script file:

sed -f script_file List_employees_a.csv > List_employees.csv

where the content of script_file is:

s/'/''/g
like image 34
kojiro Avatar answered Jul 09 '26 04:07

kojiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!