Hi,
I am trying to replace a specific column values in a csv file with double quotes when I am find embedded spaces with in the fields.
Example:
SNO,NAME,ZIPCODE,RANK,SEX,ADDRESS
1,Robert,74538,12,34, M,Robert Street, NY
2,Sam,07564,13,M,12 Main Ave, CA
3,Kim, Ed,12345,14,M,123D , MN
Desired Output:
SNO,NAME,ZIPCODE,RANK,SEX,ADDRESS
1,Robert Ken,74538,12,"34, Robert Street, NY"
2,Sam Mik,"07564",13,"12 Main Ave, CA"
3,"Kim, Ed",12345,14,"123D , MN"
As per my requirement, I was able 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 find a embedded comma with in the NAME.
Can someone tell me how to handle the embedded spaces(spaces can be one or many) and comma with in a field value as per the above example in the ADDRESS field.
following code was able to handly ZIPCODE and NAME
sed -e 's/,\(0[0-9]\)/,\"\1\"/g' -e 's/,\([A-Za-z], [A-Za-z]*\),/,\"\1\",/g' tempfile.csv > file.csv
Thanks :)-
---------- Post updated at 02:13 PM ---------- Previous update was at 12:07 PM ----------
I got it...
sed -e 's/,\(0[0-9]\)/,\"\1\"/g' -e 's/,\([ 0-9A-Za-z], [ 0-9A-Za-z]\),/,\"\1\",/g' -e 's/,\([ 0-9A-Za-z], [ 0-9A-Za-z]*\),/,\"\1\",/g' file.csv
> tempfile.csv
Thanks!!