Eshan_k has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am a seeker of perl wisdom :) Can anyone help on this ? I have following input text:

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

I am doing regex match and detecting this info: LR_PLANE_Y As soon as I detect this I want to print

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

Like this for: LR_PLANE_U This is info:

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

and for :LR_PLANE_V This is info:

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

How can I associate this next filter information, and filter type after detecting a plane? so far I am setting a flag after I detect the plane :

Currently after detecting plane it prints out filter info for all lines.How to make sure first encountered filter info belongs to previously detected plane ?

if ($line =~ /\s*AED>\sLR_PLANE_Y/i){ $y_filter_flag = 1; if ($line =~ /\s*AED>\sLR_FILTER_TYPE:\s(\w+)/i){ #Lr_params_Y +0 if ($1 eq "RESTORE_WINNER") {$y_filt +er_type="01";} } if ($line =~ /\s*WIENER_VFILTER_0:\s(-?\d+),\sWIENER_VFILTER_1:\s(-?\d ++),\sWIENER_VFILTER_2:\s(-?\d+)/i){ ##doing dome manupulation }

Currently after detecting plane it prints out filter info for all lines.How to make sure first encountered filter info belongs to previously detected plane ?

Replies are listed 'Best First'.
Re: Print specific data based on regex search in previous line
by Athanasius (Archbishop) on May 08, 2018 at 03:38 UTC

    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,

      Thank you so much Athanasius for your suggestion. The solution worked for me.
Re: Print specific data based on regex search in previous line
by NetWallah (Canon) on May 08, 2018 at 05:28 UTC
    one-liner version:
    perl -n -E 'BEGIN{ $/=" LR_PLANE"}; m/FILTER/ and print "$_\n" for sp +lit "\n";print "\n"' Your_file.txt

                    Memory fault   --   brain fried

A reply falls below the community's threshold of quality. You may see it by logging in.