Houssem Hariz Houssem Hariz - 1 year ago 68
Linux Question

Replace Double quote with double double quote with awk

I am writing a script with awk to replace all double quote with double double quote for all files in folder.

I found this in stackoverflow but i get other result

awk 'BEGIN{FS=OFS="#"} {for (i=0;i<=NF;i++) gsub(/"/, "&&",$i)} 1 $f3 > $f2

the output for this example example :

01##"hello world"98##


01##""""hello world""""98##

And I would like to get

01##""hello world""98##

Answer Source

To replace all double quotes with two double quotes, use sed:

sed 's/"/""/g' file

No need for anything fancier than that.

To do this on all files in a directory, use the in-place option if your version of sed supports it:

sed -i.bak 's/"/""/g' *

This creates backups of each file with the suffix .bak.

If you can't use -i, then use a loop with a temporary file:

for i in *; do 
    sed 's/"/""/g' "$i" > tmp && mv tmp "$i"

Or there's always good old ed:

for i in *; do
    ed -s "$i" <<< $',s/"/""/g\nw'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download