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

Re: Need to extract a particular block of lines between two patterns

by kcott (Archbishop)
on Nov 10, 2017 at 09:20 UTC ( [id://1203104]=note: print w/replies, xml ) Need Help??


in reply to [Solved]Need to extract a particular block of lines between two patterns

G'day chengchl,

Welcome to the Monastery.

Here's a generic solution for your problem. It handles:

  • Extraction of any block (i.e. there's no hard-coded or constant block number).
  • Extraction of multiple blocks.
  • Blocks of lines actually containing (plural) lines.
  • Rogue START or END tokens within START-END blocks.
  • Specification of wanted blocks in any order.
  • Invalid block specifications (e.g. out of range and non-integer identifiers).

In production code, you may want to add some form of validation and sanity checking, such that the function is short-circuited if no valid blocks are specified (which could mean not even having to open the input file).

The following shows the technique (specifically for testing via the command line); you'll need to adapt this to your needs (e.g. change <DATA> to <$fh_r>). I've embedded test data to check all the things I've said it handles; you should create your own test data, which more realistically reflects your actual data, and use that for any proof-of-concept or regression tests.

#!/usr/bin/env perl use strict; use warnings; my %print_block = map { $_ => 1 } @ARGV; my $found_block = 0; while (<DATA>) { next unless /^START$/ .. /^END$/; ++$found_block, next if /^START$/; next if /^END$/; print if $print_block{$found_block}; } __DATA__ ... line BEFORE any wanted blocks ... START block A line 1 block A line 2 with rogue END token block A line 3 block A line 4 with rogue START token block A line 5 END ... line BETWENN any wanted blocks ... START block B line 1 block B line 2 with rogue START token block B line 3 block B line 4 with rogue END token block B line 5 END ... line BETWENN any wanted blocks ... START block C line 1 block C line 2 with rogue END token block C line 3 block C line 4 with rogue START token block C line 5 END ... line BETWENN any wanted blocks ... START block D line 1 block D line 2 with rogue START token block D line 3 block D line 4 with rogue END token block D line 5 END ... line AFTER any wanted blocks ...

Some example test runs (the script name is pm_1202989_flip_flop_selection.pl):

$ pm_1202989_flip_flop_selection.pl $ pm_1202989_flip_flop_selection.pl 99 $ pm_1202989_flip_flop_selection.pl A B C $ pm_1202989_flip_flop_selection.pl 1 block A line 1 block A line 2 with rogue END token block A line 3 block A line 4 with rogue START token block A line 5 $ pm_1202989_flip_flop_selection.pl 1 4 block A line 1 block A line 2 with rogue END token block A line 3 block A line 4 with rogue START token block A line 5 block D line 1 block D line 2 with rogue START token block D line 3 block D line 4 with rogue END token block D line 5 $ pm_1202989_flip_flop_selection.pl 3 4 2 # NOTE: specified order irre +levant block B line 1 block B line 2 with rogue START token block B line 3 block B line 4 with rogue END token block B line 5 block C line 1 block C line 2 with rogue END token block C line 3 block C line 4 with rogue START token block C line 5 block D line 1 block D line 2 with rogue START token block D line 3 block D line 4 with rogue END token block D line 5 $

[Side note: As you're new here, you may have been surprised by certain responses. You can safely ignore these; a quick perusal of the "Worst Nodes" page should explain why.]

— Ken

Replies are listed 'Best First'.
Re^2: Need to extract a particular block of lines between two patterns
by chengchl (Acolyte) on Nov 11, 2017 at 01:41 UTC

    Hi Ken

    Thank you so much for the help and the kind side note. I really appreciate it!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-19 17:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found