replace value with double quotes of specific coulmn value in csv file

Hi,

I am trying to replace a specific column values in a csv file with double quotes.

Example:

SNO,NAME,ZIPCODE,RANK
1,Robert,74538,12
2,Sam,07564,13
3,Kim, Ed,12345,14

Desired Output:

SNO,NAME,ZIPCODE,RANK
1,Robert Ken,74538,12
2,Sam Mik,"07564",13
3,"Kim, Ed",12345,14

I would like to replace the zipcode value with double quotes when I find a leading zeros for the zipcode. Also, I would like to replace the name in double quotes when I found a comma with in the name.

Please throw your ideas :slight_smile:

Thanks,
moris.

This should do:

#  sed -e 's/,\(0[0-9]*\)/,\"\1\"/g'  -e 's/,\([A-Za-z]*, [A-Za-z]*\),/,\"\1\",/g' file
1,Robert,74538,12
2,Sam,"07564",13
3,"Kim, Ed",12345,14

HTH

Hi,

Thanks!!

I can see the results exactly as desired. can you please tell me how I can overwrite to the original file in the shell script..

# sed -e 's/,\(0[0-9]\)/,\"\1\"/g' -e 's/,\([A-Za-z], [A-Za-z]*\),/,\"\1\",/g' file

Thanks once again :)-

-Moris

---------- Post updated at 02:05 PM ---------- Previous update was at 12:24 PM ----------

I got it thanks...all I have to do is to redirect to a file.

---------- Post updated at 04:43 PM ---------- Previous update was at 02:05 PM ----------

Any idea on how to handle patten matching on blank space.

Ex: 34 Robert

I am not sure about the number of occurances of blank spaces between 34 and Robert so any idea on like to how to check something for the blank spaces.

while(<DATA>){
	s/(?:([a-zA-Z]*, [a-zA-Z]*)|((?<=,)(0[0-9]+)(?=,[0-9]+$)))/'"'.(($1 eq "")?$2:$1).'"'/eg;
	print;
} 
__DATA__
SNO,NAME,ZIPCODE,RANK
1,Robert Ken,74538,12
2,Sam Mik,07564,13
3,Kim, Ed,12345,14