title: Read, sort and split text file into blocks
body: <p>I am trying to re-activate my php knowledge for the following task:
  I have a larger textfile containing unsorted lines of comma separated informations, each value enclosed by a '"'.
  Each line can be understood as a single dataset, the first value of the line tells me in which table the row belongs. </p>
  
  <p>Now I need to read the file, sort the lines (so that the lines belonging to the same table are together), detect the different blocks and save them in seperate text files. After that, I can do a fast import into a mysql database using load data from infile..</p>
  
  <p>So, I can open the file and sort the lines via this:</p>
  
  <pre><code>&lt;?php
  $lines = file("importfile_unsorted.txt");
  natsort($lines);
  file_put_contents("importfile_sorted.txt", implode($lines));
  ?&gt;
  </code></pre>
  
  <p>This works. But now I get stucked. importfile_sorted.txt looks like this:</p>
  
  <pre><code>"AV1","0","0","0","0","0","0","0","0","0","0","0:0","0:0","0:0"
  "AV2","0","0","0","0","0","0","0","0","0","0","0:0","0:0","0:0"
  </code></pre>
  
  <p>.... [this would be the first block, all these lines should be saved in "av.txt"
  In the next line the new block "F" begins with several lines:</p>
  
  <pre><code>"F1","D","D","Deutsch",,,"0","W"
  "F4","E","E","Englisch",,,"0","W"
  "F7","K","K","Kath.Religionslehre",,,"0","W"
  "F8","Ev","Ev","Evang.Religionslehre",,,"0","W"
  "F9","Eth","Eth","Ethik",,,"0","W"
  </code></pre>
  
  <p>... [save all these lines beginning with Fxx into file f.txt and go to the next blocks]</p>
  
  <pre><code>"G1","PhL","PÜG"
  "G2","ChL","ChÜ"
  </code></pre>
  
  <p>..</p>
  
  <pre><code>"K1","5a","5a",,"304","Ma","Wei","0","16","16","5",,,,,"1","1","0",,"0","0","0","0"
  "K2","5b","5b",,"303","Wo","Hm","0","32","16","5",,,,,"1","1","0",,"0","0","0","0"
  "K3","5c","5c",,"302","Gr","Ro","0","32","16","5",,,,,"1","1","0",,"0","0","0","0"
  </code></pre>
  
  <p>... and so on. Later, there are blocks with a fixed first column like this:</p>
  
  <pre><code>"PL","Di 1","Ba","Q12","Inf1","CoR1"
  "PL","Di 1","Bb","Q12","F","Ü2"
  "PL","Di 1","Eg","Q12","L","M23"
  </code></pre>
  
  <p>...
  and</p>
  
  <pre><code>"PLS","Di 1","Am"," frei "
  "PLS","Di 1","Bad"," ----"
  "PLS","Di 1","Bk"," frei "
  </code></pre>
  
  <p>...
  followed by several other blocks (L1... L97, M, R1... R40, U1... U560).</p>
  
  <p>I know all possible "identifiers" (AVx, Fx, Gx, .. PL, PLS..) of the blocks, but it is also possible that a block is omitted and the input file does not a single line of it at all. 
  The input file contains about 4000 lines all together, so performance should not be too low (although it's not time-critical, the import is done maybe 10 times a year..).</p>
  
  <p>So, is there a way of getting this done in a "smart" and fast way or should I read the input file line by line, detect and remember the first value, add the current line to a result string and loop until a new first value occurs?</p>
  
  <p>Thanks for your help!
  Heiko</p>