I have data in the following format -
4,"abc"
8,"def"
9,"ghi"
I want to surround the value of the first column by double quotes.
"4","abc"
"8","def"
"9","ghi"
How do I do that ?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityA very simple substitution is:
awk -F, '{sub($1, "\"&\""); print}' tmp.csv
(Thanks evilsoup).
The option -F, tells awk that the field delimiter is the comma, the argument of sub(stitution) tells awk to replace the first field with itself (&), preceded and followed by ".
This is just a quick and dirty way (there will be better ways):
sed -e 's/"//g' -e 's/^\|$/"/g' -e 's/,/","/g' file.csv
The first s/"//g
removes all "
.
Then s/^\|$/"/g
adds a "
at the beginning and end of the line.
And last s/,/","/g
replaces all ,
by ","
.
Of course this only works if you don't have "
and ,
in your fields.