Loading ...
Sorry, an error occurred while loading the content.

49772Re: comma-separated fields - conversion, 1 line to N

Expand Messages
  • Suresh Govindachar
    Apr 30 5:06 PM
      Jacob Lerner
      Sent: Friday, April 30, 2004 2:25 PM

      [how to split up lines such as:

      country1,code11
      country2,code21,code22,...,code2N
      country3,code31,code32

      into the lines:

      country1,code11
      country1,code21
      country2,code22

      etc.

      awk solution
      awk -F, '{country=$1; for(i=2; i<=NF; i++) print $1 " " $(i); }'

      How to do it in vim ?]

      Here's solution based on recursion that worked on my test case.

      First issue the following three lets:
      :let @a='s/^\(.\{-}\),\(.\{-}\),\(.*\)/\1,\2\r\1,\3/c'
      :let @/='^\(.\{-}\),\(.\{-}\),\(.*\)'
      :let @b='execute "normal kn"|@a|@b'
      Then just do it with the command
      :2@b

      (By the way, Jacob Lemer also wrote:

      > doesn't have awk or perl (Win2k)

      which seems to imply that neither awk nor perl
      can be had on Win2K. I do know that perl
      can be had on Win2K, and suspect so can awk.)

      --Suresh
    • Show all 8 messages in this topic