> I would like to correct some bad lines in a csv.
>
> Empty fields of bad lines should be completed by fields of the
> previous good line that have all fields completed.
>
> foo0;bar0;foo1;bar1;foo2;bar2;foo3;bar3;foo4;bar4;foo5;bar5;
> ;;foo1;bar1;foo2;bar2;foo3;bar3;foo4;bar4;foo5;bar5;
> foo0;bar0;;;;bar2;foo3;bar3;foo4;bar4;foo5;bar5;
> fooA;barA;fooB;barB;fooC;barC;fooD;barD;fooE;barE;fooF;barF;
> foo0;bar0;;;;bar2;foo3;bar3;foo4;bar4;foo5;bar5;
>
> should be
>
> foo0;bar0;foo1;bar1;foo2;bar2;foo3;bar3;foo4;bar4;foo5;bar5;
> foo0;bar0;foo1;bar1;foo2;bar2;foo3;bar3;foo4;bar4;foo5;bar5;
> foo0;bar0;foo1;bar1;foo2;bar2;foo3;bar3;foo4;bar4;foo5;bar5;
> fooA;barA;fooB;barB;fooC;barC;fooD;barD;fooE;barE;fooF;barF;
> fooA;barA;fooB;barB;fooC;barC;fooD;barD;fooE;barE;fooF;barF;
Under the assumption, that the last line should look like this:
>foo0;bar0;fooB;barB;fooC;bar2;foo3;bar3;foo4;bar4;foo5;bar5;
this should work:
fu! <SID>CSV() range
if line('$') < 2 | return | endif
let prevline=split(getline(1), ';', 1)
let linecount=2
while linecount < line ('$')
let tline=split(getline(linecount), ';', 1)
let i=0
let status=0
for cell in copy(tline)
if empty(cell)
let tline[i]=prevline[i]
let status=1
else
let tline[i] = cell
endif
let i+=1
endfor
if status
call setline(linecount, join(tline, ';'))
endif
let prevline=tline
let linecount+=1
endw
endfu
:com! ReorderCSV :call <SID>CSV()
regards,
Christian
--
:wq
--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
No comments:
Post a Comment