pedrorijo91
pedrorijo91

Reputation: 7845

How to replace double quotes by escaped double quote in awk?

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

Answers (2)

JosEduSol
JosEduSol

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

Jose Ricardo Bustos M.
Jose Ricardo Bustos M.

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

Related Questions