Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: command line perl command to get between lines with non greedy match

by tybalt89 (Monsignor)
on Jan 18, 2020 at 20:47 UTC ( [id://11111586]=note: print w/replies, xml ) Need Help??


in reply to command line perl command to get between lines with non greedy match

Here's one that uses almost no storage, by seeking back to the last PATTERN1 and re-reading the input file. Note that this will not work on a pipe.

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11111545 use warnings; my $fh = *DATA; # FIXME to your input file, DATA only used for testing my $lastpattern1; while( <$fh> ) { if( /PATTERN1/ ) { $lastpattern1 = tell($fh) - length $_; } elsif( $lastpattern1 and /PATTERN3/ ) { seek $fh, $lastpattern1, 0; while( <$fh> ) { my $end = s/ (?=PATTERN3)/\n\n/; print; $end and last; } $lastpattern1 = undef; } } __DATA__ PATTERN1 SOME INFO TEXT1 TEXT2 TEXT3 PATTERN2 SOME INFO PATTERN1 SOME INFO TEXT4 TEXT5 TEXT6 PATTERN3 SOME INFO PATTERN1 SOME INFO TEXT1 TEXT2 TEXT3 PATTERN4 SOME INFO PATTERN1 SOME INFO TEXT4 TEXT55 TEXT6 PATTERN3 SOME INFO

I also do the fix up on the PATTERN3 line, though I'm curious if that was just a typo on your part?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11111586]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 06:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found