Wednesday, December 16, 2009

Re: Rectify some bad csv

On 15 déc, 19:39, Tim Chase <v...@tim.thechases.com> wrote:
> > I would like to correct some bad lines in a csv.
>
>
> First of all, it would help to have good example data, since in
> your last line, it has valid values like "foo0" as the first
> element that has magically changed to "fooA" in the
> result-set...same with "bar5" -> "barF" as the penultimate element.
>
> <pedant>Second, it's only vaguely a "CSV" -- those are
> semi-colons instead of commas. </pedant>
>
> That said, however, it can also be done in vim7+ with this
> hideous one-liner:
>
> :2,$g/;;/let p=split(getline(line('.')-1), ';', 1) | let
> c=split(getline('.'), ';', 1) | s/.*/\=join(map(range(len(c)),
> 'strlen(c[v:val]) ? (c[v:val]) : (p[v:val])'), ';')
>
> (c=current line; p=previous line)
>
> I don't envy any person trying to deconstruct that -- it was hard
> enough to assemble :)  It does choke if the previous line doesn't
> have enough elements to populate the current line, but that's
> somewhat to be expected.

Your solution seems powerful.

My example was not so good. This is a new :

Several cases are to be resolved :

First case
____________
foo1;foo2;foo3;;;;;
;foo2;foo3;;;;;

Has to become
foo1;foo2;foo3;;;;;
foo1;foo2;foo3;;;;;

Second case
____________
foo1;foo2;foo3;;;;;
foo1;;;;;;;

Has to become
foo1;foo2;foo3;;;;;
foo1;foo2;foo3;;;;;

Third case
____________
foo1;foo2;foo3;;;;;
foo1;;foo3;;;;;

Has to become
foo1;foo2;foo3;;;;;
foo1;foo2;foo3;;;;;

NB : When u use global command it seems missing some /

--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments: