emraldinho
emraldinho

Reputation: 497

replace single quote and double quote in files in linux

I took a dump from postgres which created a bunch of CSVs, which have single (') and double (") characters in a few huge files.

How can I replace these with \" and \' in linux?

Thanks!

Upvotes: 0

Views: 254

Answers (1)

anubhava
anubhava

Reputation: 785721

You can use sed:

sed "s/['\"]/\\\\&/g" <<< "abc'def',\"foo\",bar"

abc\'def\',\"foo\",bar

If you want to avoid escaping already escaped quotes then use:

sed -E "s/(^|[^\\])(['\"])/\\1\\\\\\2/g" file

Upvotes: 2

Related Questions