4

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 ?

3 Answers 3

8

A 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 ".

2
1

This might work for you (GNU sed):

sed 's/[^,]*/"&"/' file
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .