CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Frequently Asked Questions:
How can I pull out lines between two patterns that

 



Jasmine
Administrator

Mar 15, 2001, 5:55 AM

Post #1 of 1 (12817 views)
How can I pull out lines between two patterns that Can't Post

How can I pull out lines between two patterns that are themselves on different lines?

You can use Perl's somewhat exotic .. operator (documented in ]url=http://www.perlguru.com/perldocs/5.005_02/pod/perlop.html]the perlop manpage):


Code
    perl -ne 'print if /START/ .. /END/' file1 file2 ...

If you wanted text and not lines, you would use


Code
    perl -0777 -pe 'print "$1\n" while /START(.*?)END/gs' file1 file2 ...

But if you want nested occurrences of START through END, you'll run up against the problem described in the question in this section on matching balanced text.

Here's another example of using ..:


Code
    while (<>) { 
$in_header = 1 .. /^$/;
$in_body = /^$/ .. eof();
# now choose between them
} continue {
reset if eof(); # fix $.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives