Perl - how to grep a block of text from a file -


it can in xml or text format. how in general grep block of text in perl?

<track type="ws">       <range>        <rangestart>0</rangestart>        <rangeend>146.912</rangeend>        <locationindex>0</locationindex>        <propertyindex>0</propertyindex>       </range> </track> <track type="ps" id="1">       <range>        <rangestart>0</rangestart>        <rangeend>146.912</rangeend>        <locationindex>1</locationindex>        <propertyindex>1</propertyindex>       </range> </track> 

i want grep type="ps" , till </range>.

one solution open file, read line line , match block.

open(fh, "file.txt"); foreach $line (<fh>) {     if ($line =~ m/type="cc"(.*?)<\/range>/) {         print $1;     } } 

but there more optimal solution without reading file line line?

bjørn absolutely right xml. more general question, might interested in 1 of favorite per one-liners:

perl -ne 'print if /type="cc"/../<\/range>/' input.txt 

Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -