Using gsub to replace a double quote with two double quotes?

I'm trying to write a script in awk that (by the way) replaces all instances of double quotes with two double quotes in a row. But for some reason I can't seem to escape the double quotes.

So for example:

Mary had a "little" lamb

      

will become

Mary had a ""little"" lamb

      

Thanks, kindly!

+2


source to share


1 answer


You can do it like this:

echo 'Mary had a "little" lamb' | awk '{ gsub(/"/, "&&") } 1' 

      

Output:

Mary had a ""little"" lamb

      



1

calls awk default block at the end { print $0 }

.

It's even shorter with sed:

echo 'Mary had a "little" lamb' | sed 's/"/&&/g'

      

+3


source







All Articles