Wednesday, December 16, 2009

Re: Rectify some bad csv

epanda wrote:
> On 15 déc, 19:39, Tim Chase <v...@tim.thechases.com> wrote:
>> :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])'), ';')
>
> Several cases are to be resolved :
>
> First case
> ____________
> foo1;foo2;foo3;;;;;
> ;foo2;foo3;;;;;
>
> Has to become
> foo1;foo2;foo3;;;;;
> foo1;foo2;foo3;;;;;

works...

> Second case
> ____________
> foo1;foo2;foo3;;;;;
> foo1;;;;;;;
>
> Has to become
> foo1;foo2;foo3;;;;;
> foo1;foo2;foo3;;;;;

also works...

> Third case
> ____________
> foo1;foo2;foo3;;;;;
> foo1;;foo3;;;;;
>
> Has to become
> foo1;foo2;foo3;;;;;
> foo1;foo2;foo3;;;;;

also works...

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

Perhaps if my command doesn't work for you, you copied it wrong?
Or have a stripped-down version of Vim?

The :g command is simply

:2,$g/;;/actions

It could be issued as

:2,$g/^/

which might fix some edge cases (where the only missing/blank
element is at the BOL/EOL). The actions roughly break down into

- store an array of the previous line (let p=)
- store the current line as an array (let c=)
- replace the contents of the current line ("s/.*/\=big ugly
mess") with the results of joining "each element from this line
if it exists, or the corresponding element from the previous
line" back together with semicolons

The 3 actions are separated by "|" in case you're having trouble
identifying them.

-tim


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

No comments: