Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to split a .CSV file using a key

How to split a .CSV file using a key

Scheduled Pinned Locked Moved Database
comtoolstutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    ptrckmc249
    wrote on last edited by
    #1

    Someone else asked this quesion, I can't remember where to reply to his post. But, since I wrote the script already, wanted to post to this good site. The code is written in biterscripting. (http://www.biterscripting.com) I would like to split a csv file into secondary csv files on a key field. Ex : all.csv (1st column is the key field for splitting action) P1;X;10 P1;Y;5 P2;Z;10 P2;A;5 P3;B;30 P3;C;60 P1.csv P2.csv P3.csv P1;X;10 P2;Z;10 P3;B;30 P1;Y;5 P2;A;5 P3;B;60 Here is the script. Patrick # START OF SCRIPT var str saved_wsep set $saved_wsep = $wsep set $wsep=";" var str output_file var str input, output, row_str, c1, prev_c1 # c1 for column 1 cat "all.csv" >$input var int row, rows set $rows = { len $input } set $row = 1 while ($row <= $rows) do lex -p $row $input >$row_str wex -p "1" $row_str >$c1 if ($c1 <> $prev_c1) do # new output file begins. Is there anything in the previous file. if ($prev_c1 <> "") do echo $output > { echo $prev_c1 ".csv" } done endif set $prev_c1 = $c1 done endif set $output = $output + $row_str + "\n" set $row = $row + 1 done echo $output > { echo $prev_c1 ".csv" } set $wsep = $saved_wsep

    W 1 Reply Last reply
    0
    • P ptrckmc249

      Someone else asked this quesion, I can't remember where to reply to his post. But, since I wrote the script already, wanted to post to this good site. The code is written in biterscripting. (http://www.biterscripting.com) I would like to split a csv file into secondary csv files on a key field. Ex : all.csv (1st column is the key field for splitting action) P1;X;10 P1;Y;5 P2;Z;10 P2;A;5 P3;B;30 P3;C;60 P1.csv P2.csv P3.csv P1;X;10 P2;Z;10 P3;B;30 P1;Y;5 P2;A;5 P3;B;60 Here is the script. Patrick # START OF SCRIPT var str saved_wsep set $saved_wsep = $wsep set $wsep=";" var str output_file var str input, output, row_str, c1, prev_c1 # c1 for column 1 cat "all.csv" >$input var int row, rows set $rows = { len $input } set $row = 1 while ($row <= $rows) do lex -p $row $input >$row_str wex -p "1" $row_str >$c1 if ($c1 <> $prev_c1) do # new output file begins. Is there anything in the previous file. if ($prev_c1 <> "") do echo $output > { echo $prev_c1 ".csv" } done endif set $prev_c1 = $c1 done endif set $output = $output + $row_str + "\n" set $row = $row + 1 done echo $output > { echo $prev_c1 ".csv" } set $wsep = $saved_wsep

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Instead of posting this to a forum, you could write an article of it. Articles are the intended way to share solutions and forums for questions.

      The need to optimize rises from a bad design.My articles[^]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups