From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: Date: Fri, 2 Jun 2006 11:09:58 +0800 From: "Rogelio Serrano" To: "Fans of the OS Plan 9 from Bell Labs" <9fans@cse.psu.edu> Subject: Re: [9fans] csv files -> embarrasing In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: Topicbox-Message-UUID: 590e04f6-ead1-11e9-9d60-3106f5b1d025 On 4/28/06, Steve Simon wrote: > Ok, I have spent half an hour trying to parse CSV files > and it's getting embarrasing, I could do it in C but I should > be able to use rc + sed + awk. > > The problem is that some of my CSV files fields contain whitespace > and thus have double quotes around them. > > I thought rc knows about %q quotes strings so I could use it to > do my parsing, but it fails, can this be done, or is C the answer? > seems a shame to resort to sledge hammers. > > -Steve > > cpu% cat file.csv > a,b,"c,d,e",f,g > p,q,r,s,t > > cpu% > cpu% cat extract > #!/bin/rc > > sed 's/"([^"]*)"/''\1''/g; s/,/ /g' $* | > while (s=`{read}) > echo $s(1) $s(3) $s(4) > > > cpu% extract file.csv > a 'c d > p r s > > I deal with csv files everyday at work and i just use custom plain c parsers. Reinventing the wheel and all. -- www.smsglobal.net SMS Global Ltd Short Message Service For Seafarers