title: Perl - how to grep a block of text from a file
body: <p>It can be in XML or any text format. How in general to grep for a block of text in Perl?</p>
  
  <pre><code>&lt;track type="ws"&gt;
        &lt;range&gt;
         &lt;rangeStart&gt;0&lt;/rangeStart&gt;
         &lt;rangeEnd&gt;146.912&lt;/rangeEnd&gt;
         &lt;locationIndex&gt;0&lt;/locationIndex&gt;
         &lt;propertyIndex&gt;0&lt;/propertyIndex&gt;
        &lt;/range&gt;
  &lt;/track&gt;
  &lt;track type="ps" id="1"&gt;
        &lt;range&gt;
         &lt;rangeStart&gt;0&lt;/rangeStart&gt;
         &lt;rangeEnd&gt;146.912&lt;/rangeEnd&gt;
         &lt;locationIndex&gt;1&lt;/locationIndex&gt;
         &lt;propertyIndex&gt;1&lt;/propertyIndex&gt;
        &lt;/range&gt;
  &lt;/track&gt;
  </code></pre>
  
  <p>I want to grep for <code>type="ps"</code> and get everything till the <code>&lt;/range&gt;</code>.</p>
  
  <p>One solution is to open the file, read it line by line and then match the block.</p>
  
  <pre><code>open(FH, "file.txt");
  foreach $line (&lt;FH&gt;) {
      if ($line =~ m/type="cc"(.*?)&lt;\/range&gt;/) {
          print $1;
      }
  }
  </code></pre>
  
  <p>but is there a more optimal solution without reading the file line by line?</p>