Code:
% gawk -F',' ' {
if(NF==5) { if($3 ~ /^0/) { $3 = "\"" $3 "\"" } print $0 }
else {
sno=$1
if($3 !~ /^[0-9]*$/) {
name="\"" $2 "," $3 "\""; zip=$4;
if($(NF-1) !~ /^[0-9]*$/) {
care_of="\"" $(NF-1) "," $(NF) "\""; rank=$(NF-2);
} else { care_of=$6;rank=$5; }
} else {
if($3 ~ /^0/) { $3 = "\"" $3 "\"" }
name=$2; zip=$3;
if($(NF-1) !~ /^[0-9]*$/) { care_of="\"" $(NF-1) "," $(NF) "\""; rank=$(NF-2); } else { care_of=$5;rank=$4; }
}
if(zip ~ /^0/) { zip = "\"" zip "\"" }
print sno "," name "," zip "," rank "," care_of
}
} ' inputFile.csv > outputFile.csv
% cat *csv
SNO,NAME,ZIPCODE,RANK,CARE_OF
1,Robert Ken,74538,12,RICHARD JOHNSON, P.C
2,Sam Mik,07564,13,% R.S MIKE, V.K.S
3,Kim, Ed,12345,14,@90 KMS, %TK
SNO,NAME,ZIPCODE,RANK,CARE_OF
1,Robert Ken,74538,12,"RICHARD JOHNSON, P.C"
2,Sam Mik,"07564",13,"% R.S MIKE, V.K.S"
3,"Kim, Ed",12345,14,"@90 KMS, %TK"