sed -e 's/=\([^,]*\)/="\1"/g' file.txt
awk -F, -v OFS=, '{for (f=1;f<=NF;f++) {sub(/=/,"&\"",$f); sub(/$/,"\"",$f)}; print}' file.txt
Or, shorter:
awk -F, -v OFS=, '{for (f=1;f<=NF;f++) {gsub(/=|$/,"&\"",$f)} print}' file.txt
Shorter still:
awk -F, -v OFS=, '{for (f=1;f<=NF;f++) {gsub(/=|$/,"&\"",$f)}} 1' file.txt
Using ex
Ex is designed for file editing, but you can preview changes without actually saving them back to the file like so:
ex -sc '%s/=\([^,]*\)/="\1"/g | %p | q!' file.txt
To actually make the changes and save them to the file, use:
ex -sc '%s/=\([^,]*\)/="\1"/g | x' file.txt
However if you give a pattern which is found nowhere in the file (e.g. there is no =
anywhere in the file) then Ex will not exit automatically. Thus for better robustness I usually use printf
to pass commands to Ex:
printf '%s\n' '%s/=\([^,]*\)/="\1"/g' %p | ex file.txt
And to save changes:
printf '%s\n' '%s/=\([^,]*\)/="\1"/g' x | ex file.txt