Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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

by AnomalousMonk (Bishop)
on Jan 17, 2020 at 21:51 UTC ( #11111551=note: print w/replies, xml ) Need Help??

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

Please use  <code> ... </code> code tags for code, command line invocations, error messages and input/output data. Please see Writeup Formatting Tips, Markup in the Monastery and How do I post a question effectively? (and hopefully also How do I change/delete my post?). Please see also Short, Self-Contained, Correct Example.

Something like this (insofar as I understand your input) can be done as a command line one-liner, but it gets messy and I prefer to write a script for it, something like
    perl  < input.file  > output.file
that might look like (untested | semi-tested):

use strict; use warnings; my $rx_start = qr{ \A \s* PATTERN1 }xms; my $rx_stop = qr{ \A \s* PATTERN3 }xms; my @records; RECORD: while (my $record = <STDIN>) { if ($record =~ $rx_start) { # push @records, $record; # UPDATE: NO: still "greedy" extracti +on of records/lines @records = $record; # UPDATE: FIXED: only extracts BETWEE +N start/stop patterns next RECORD; } if ($record =~ $rx_stop) { print @records, $record; @records = (); next RECORD; } push @records, $record if @records; } exit;

Update: Example code fixed to extract records only between the start/stop patterns. Any reformatting of output that may be needed is still not addressed.

Give a man a fish:  <%-{-{-{-<

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2020-02-28 22:05 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (127 votes). Check out past polls.