In my bash script, I am trying to replace all apostrophes(') with two apostrophes('') in a csv file in order to commit it to a postgres database. As you know all single quotes in a postgres query need to be escaped with an apostrophe.
I can't use double quotations around my variables in my query because they also have double quotes in them too. So there is no easy way out apart from doing a blanket replace using sed. I have experimented with the following ways but to no avail:
sed "s/\'/\'\'/g" test.txt #Does not work
sed "s/'/''/g" test.txt #Does not work
sed s/\'/"\'\'"/g test.txt #Does not work
Does anyone have ideas on how I can get this to work?
This will work for you:
Since you are using double quotes to enclose the command, the apostrophe doesn't need to get escaped.
Btw, if you want to use single quotes to enclose the command itself, you may use the octal representation of the apostrophe
\047in the command: