zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: Parsing CVS files
Date: Sat, 02 Feb 2019 20:14:25 +0000	[thread overview]
Message-ID: <157d7b5d59005133ae4722f5c452db6aa0078547.camel@ntlworld.com> (raw)
In-Reply-To: <20190202185911.vsmvvsp5df3yaa3z@gmx.de>

> I'm looking for an easy way to split the lines of a .csv file into
> the fields of an array variable.  There's a script that does that
> somewhore on the net.  But that script parses lines character by
> character and just manages to parse about 100 (long) lines per
> second.
> 
> Fields in a .csv file are separated by commas, *but* commas
> between a pair of quotes do not split.  Or phrased differently:
> Commas that have an even number of double quotes left of them do
> split, but commas with an uneven number left of then don't split.
> 
> Any ideas for a quick implementation?

Sebastian has done similar things so may have better ideas.

If you're happy to use shell syntax --- in other words, the other forms
of quoting are active, not just double quotes, so backslashes and single
quotes might do inconvenient things --- and you're not too bothered
about unquoted spaces, which will add extra lines of splitting, you can
use this trick:

% line='This,"is, quite possibly, a",line,"of,stuff","with,commas"'
% print -rl ${(Q)${${(z)${line//,/, }}%%,}//, /,}
This
is, quite possibly, a
line
of,stuff
with,commas

Each comma gets a space added, then the line is split on syntactically
active spaces; any comma at the end of a field is removed; the remaining
commas are restored.

To strip the quotes, add the (Q) flag to the outermost step.

If you need to be careful about unquoted spaces, you need to be
cleverer:  e.g. backslash quote them and then remove the bacslashes
later.  E.g. up to subtle effects associated with backslashes

print -rl ${${${${(z)${${line// /\\ }//,/, }}%%,}//, /,}//\\ / }

will retain existing spaces.

Also, if you want to keep empty fields, you'll need the final result
to use "${(@}this}".  Probably easiest to assign to an array as otherwise
the quotes will affect the substitution.

If you're worried about subtle effects with backslashes, I don't think
you're ever going to be satisfied with a quick and dirty hack like this,
so you'll have to decide how sophisticated you need to be.

pws


  reply	other threads:[~2019-02-02 20:26 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-02 18:59 Dominik Vogt
2019-02-02 20:14 ` Peter Stephenson [this message]
2019-02-03  4:19   ` Sebastian Gniazdowski
2019-02-05 21:30     ` Dominik Vogt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=157d7b5d59005133ae4722f5c452db6aa0078547.camel@ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).