> I have a csv file with the following content:
> 
> country X
> aa
> bb
> cc
> dd
> country Y
> ee
> ff
> 
> which I would like to convert to 
> 
> X aa
> X bb
> X cc
> X dd
> Y ee
> Y ff
> 
> Can this be done with search and replace? I have got a search pattern
> but don't manage to churn out multiple lines.

It might be doable in a more complex s&r, but I'd do it in two 
passes:

1) bring the "country ___" line down to each non-country line:

   :v/^country /?country ?t-|s/country //|j


2) delete the country lines that are now superfluous:

   :g/^country /d


Step #1 is a bit packed:

   :v/          on every line that doesn't match
   country      the literal text "country "
   /            perform this set of actions:
     ?country ?   look backwards to the most recent
                  line containing "country "
     t            and copy it
     -            to the line before this match
                  ("-" is short-hand for ".-1")
     |            and then
     s/           substitute
     country      the first "country " on the line
     //           replacing with nothing
     |            and then
     j            join this line (the actual country data)
                  with the next line (the one to which we
                  are prepending, e.g. "cc")

Hope this helps both solve the problem and give understanding to 
my solution.

-tim




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

Reply via email to