Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Print specific data based on regex search in previous line

by Athanasius (Archbishop)
on May 08, 2018 at 03:38 UTC ( [id://1214187]=note: print w/replies, xml ) Need Help??


in reply to Print specific data based on regex search in previous line

Hello Eshan_k,

Here’s a straightforward approach using two variables to keep track of which plane and filter type we are “in” at any given input line:

use strict; use warnings; my $prefix = qr/ ^ \s* \w{3} > \s+ /x; my ($plane, $filter); while (my $line = <DATA>) { if ($line =~ / $prefix LR_PLANE_(\w+), /x) { $plane = $1; $filter = undef; print "\n$line"; } elsif ($plane) { if ($line =~ / $prefix LR_FILTER_TYPE: \s+ RESTORE_(\w+) /x) { $filter = $1; print $line; } elsif ($filter && $line =~ / $prefix $filter _ [VH] FILTER_ \d +: /x) { print $line; } } } __DATA__ ABC> LR_PLANE_Y, LRU_SIZE: 128X128, LRU_X: 0, LRU_Y: 0, ABC> Dif: 744374271, Rng: 51464, Cnt: 4, Ret: 1 ABC> LR_FILTER_TYPE: RESTORE_WIENER ABC> Literal: 0 ABC> Literal: 11 ABC> Literal: 14 ABC> Literal: 0 ABC> Literal: 12 ABC> Literal: 3 ABC> WIENER_VFILTER_0: 5, WIENER_VFILTER_1: -19, WIENER_VFILTER_2: +30 ABC> WIENER_HFILTER_0: 5, WIENER_HFILTER_1: -20, WIENER_HFILTER_2: +9 ABC> LR_PLANE_U, LRU_SIZE: 128X128, LRU_X: 0, LRU_Y: 0, ABC> Dif: 525860863, Rng: 38897, Cnt: 0, Ret: 1 ABC> LR_FILTER_TYPE: RESTORE_WIENER ABC> Literal: 2 ABC> Literal: 6 ABC> Literal: 9 ABC> Literal: 7 ABC> WIENER_VFILTER_0: 0, WIENER_VFILTER_1: -2, WIENER_VFILTER_2: 2 +2 ABC> WIENER_HFILTER_0: 0, WIENER_HFILTER_1: 2, WIENER_HFILTER_2: 11 ABC> LR_PLANE_V, LRU_SIZE: 128X128, LRU_X: 0, LRU_Y: 0, ABC> Dif: 66698751, Rng: 59048, Cnt: 9, Ret: 1 ABC> LR_FILTER_TYPE: RESTORE_WIENER ABC> Literal: 14 ABC> Literal: 4 ABC> Literal: 3 ABC> Literal: 5 ABC> WIENER_VFILTER_0: 0, WIENER_VFILTER_1: -22, WIENER_VFILTER_2: +25 ABC> WIENER_HFILTER_0: 0, WIENER_HFILTER_1: -13, WIENER_HFILTER_2: +12

Output:

13:37 >perl 1886_SoPW.pl ABC> LR_PLANE_Y, LRU_SIZE: 128X128, LRU_X: 0, LRU_Y: 0, ABC> LR_FILTER_TYPE: RESTORE_WIENER ABC> WIENER_VFILTER_0: 5, WIENER_VFILTER_1: -19, WIENER_VFILTER_2: +30 ABC> WIENER_HFILTER_0: 5, WIENER_HFILTER_1: -20, WIENER_HFILTER_2: +9 ABC> LR_PLANE_U, LRU_SIZE: 128X128, LRU_X: 0, LRU_Y: 0, ABC> LR_FILTER_TYPE: RESTORE_WIENER ABC> WIENER_VFILTER_0: 0, WIENER_VFILTER_1: -2, WIENER_VFILTER_2: 2 +2 ABC> WIENER_HFILTER_0: 0, WIENER_HFILTER_1: 2, WIENER_HFILTER_2: 11 ABC> LR_PLANE_V, LRU_SIZE: 128X128, LRU_X: 0, LRU_Y: 0, ABC> LR_FILTER_TYPE: RESTORE_WIENER ABC> WIENER_VFILTER_0: 0, WIENER_VFILTER_1: -22, WIENER_VFILTER_2: +25 ABC> WIENER_HFILTER_0: 0, WIENER_HFILTER_1: -13, WIENER_HFILTER_2: +12 13:37 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Print specific data based on regex search in previous line
by Eshan_k (Acolyte) on Jun 01, 2018 at 04:39 UTC
    Thank you so much Athanasius for your suggestion. The solution worked for me.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-26 01:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found