title: Split a giant kml file
body: <p>I have a giant kml file with the following structure:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;kml xmlns="http://www.opengis.net/kml/2.2"&gt;
    &lt;Document&gt;
      &lt;Style id="transBluePoly"&gt;
        &lt;LineStyle&gt;
          &lt;width&gt;1.5&lt;/width&gt;
        &lt;/LineStyle&gt;
        &lt;PolyStyle&gt;
          &lt;color&gt;30ffa911&lt;/color&gt;
        &lt;/PolyStyle&gt;
      &lt;/Style&gt;
      &lt;Style id="labelStyle"&gt;
         &lt;IconStyle&gt;
            &lt;color&gt;ffffa911&lt;/color&gt;
            &lt;scale&gt;0.35&lt;/scale&gt;
         &lt;/IconStyle&gt;
         &lt;LabelStyle&gt;
           &lt;color&gt;ffffffff&lt;/color&gt;
           &lt;scale&gt;0.35&lt;/scale&gt;
        &lt;/LabelStyle&gt;
      &lt;/Style&gt;
      &lt;Placemark&gt;
        &lt;name&gt;9840229084|2013-03-06 13:41:34.0|rent|0.0|2|0|0|1|T|5990F529FB98F28A1F17D182152201A4|0|null|null|null|null|null|null|null|null|null|null|F|F|0|NO_POSTCODE&lt;/name&gt;
        &lt;styleUrl&gt;#transBluePoly&lt;/styleUrl&gt;
        &lt;Polygon&gt;
          &lt;outerBoundaryIs&gt;
            &lt;LinearRing&gt;
              &lt;coordinates&gt;
              -1.5191200,53.4086600
              -1.5214300,53.4011900
              -1.5303600,53.4028800
              -1.5435800,53.4033900
              -1.5404900,53.4083600
              -1.5191200,53.4086600
              &lt;/coordinates&gt;
            &lt;/LinearRing&gt;
          &lt;/outerBoundaryIs&gt;
        &lt;/Polygon&gt;
      &lt;/Placemark&gt;
      &lt;Placemark&gt;
        &lt;name&gt;9840031669|2013-03-06 13:14:22.0|rent|0.0|0|0|0|1|F|E5BAC836984F53F91D7F60F247920F0C|0|null|null|null|null|null|null|null|null|null|null|F|F|3641161|DE4 3JT&lt;/name&gt;
        &lt;styleUrl&gt;#transBluePoly&lt;/styleUrl&gt;
        &lt;Polygon&gt;
          &lt;outerBoundaryIs&gt;
            &lt;LinearRing&gt;
              &lt;coordinates&gt;
              -1.2370933,53.1227587
              -1.2304837,53.1690463
              -1.1783129,53.2226956
              -1.2016444,53.2833233
              -1.3213687,53.3248921
              -1.4809916,53.3039582
              -1.6167192,53.2438689
              -1.5593782,53.1336370
              -1.4296123,53.0962399
              -1.3205129,53.1024090
              -1.2370933,53.1227587
              &lt;/coordinates&gt;
            &lt;/LinearRing&gt;
          &lt;/outerBoundaryIs&gt;
        &lt;/Polygon&gt;
      &lt;/Placemark&gt;
  </code></pre>
  
  <p>I need to extract 1 million polygons from this to make it manageable (know geo DB is ultimate solution - looking for a quick fix).</p>
  
  <p>Loading it into a lightweight text editor and just deleting some lines would be my first port of call, but suspect this will take forever and a day (it's 10 Gb, I've got 16 Gb RAM). Just wondering if there is a more intelligent solution from the a linux terminal that avoids having to read it all into RAM. I've seen perl and bash commands for doing this but can't see how they would work for taking a random (or first million) sample: <a href="http://www.unix.com/shell-programming-scripting/159470-filter-kml-file-xml-remove-unwanted-entries.html" rel="nofollow">http://www.unix.com/shell-programming-scripting/159470-filter-kml-file-xml-remove-unwanted-entries.html</a></p>