Reputation: 7845
In awk how can I replace all double quotes by escaped double quotes?
The dog is "very" beautiful
would become
The dog is \"very\" beautiful
I've seen this answer (Using gsub to replace a double quote with two double quotes?) and I've tried to adapt it, but I'm not very good with awk (and sed is no option because I work both on Linux and OS X and they have different 'sed' installed)
Upvotes: 10
Views: 13426
Reputation: 5426
From the answer you linked:
With gsub:
echo 'The dog is "very" beautiful' | gawk '{ gsub(/"/,"\\\"") } 1'
Alternative, with sed:
echo 'The dog is "very" beautiful' | sed 's/"/\\"/g'
In both cases output is:
The dog is \"very\" beautiful
Upvotes: 14
Reputation: 8164
you may to use with GNU awk:
echo 'The dog is "very" beautiful' | gawk '{ gsub(/"/,"\\\"") } 1'
you get
The dog is \"very\" beautiful
explanation:
for special characters \
and "
, you must to use escape sequence \\
and \"
Upvotes: 4