title: Split text file into parts based on a pattern taken from the text file
body: <p>I have many text files of fixed-width data, e.g.:</p>
  
  <pre><code>$ head model-q-060.txt 
  % x                      y                        
  15.0                     0.0                      
  15.026087                -1.0                     
  15.052174                -2.0                     
  15.07826                 -3.0                     
  15.104348                -4.0                     
  15.130435                -5.0                     
  15.156522                -6.0                     
  15.182609                -6.9999995               
  15.208695                -8.0  
  </code></pre>
  
  <p>The data comprise 3 or 4 runs of a simulation, all stored in the one text file, with no separator between runs. In other words, there is no empty line or anything, e.g. if there were only 3 'records' per run it would look like this for 3 runs:</p>
  
  <pre><code>$ head model-q-060.txt 
  % x                      y                        
  15.0                     0.0                      
  15.026087                -1.0                     
  15.052174                -2.0                     
  15.0                     0.0                      
  15.038486                -1.0                     
  15.066712                -2.0                     
  15.0                     0.0                      
  15.041089                -1.0                     
  15.087612                -2.0                     
  </code></pre>
  
  <p>It's a COMSOL Multiphysics output file for those interested. Visually you can tell where the new run data begin, as the first x-value is repeated (actually the entire second line might be the same for all of them). So I need to firstly open the file and get this x-value, save it, then use it as a pattern to match with awk or csplit. I am struggling to work this out!</p>
  
  <p>csplit will do the job:</p>
  
  <pre><code>$ csplit -z -f 'temp' -b '%02d.txt' model-q-060.txt /^15\.0\\s/ {*}
  </code></pre>
  
  <p>but I have to know the pattern to split on. This question is similar but each of my text files might have a different pattern to match: <a href="https://stackoverflow.com/questions/8272017/split-files-based-on-file-content-and-pattern-matching">Split files based on file content and pattern matching</a>.</p>
  
  <p>Ben.</p>