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

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -