in reply to Re^2: Reading multiple lines from text file
in thread Reading multiple lines from text file

Hello newperlbie, and welcome to the Monastery!

Here’s a variation on choroba’s solution which uses the flip-flop operator and a single flag:

use strict; use warnings; my $in = 0; while (<DATA>) { if (/ dev$/ .. / manage$/) { $in = 1; print; } elsif ($in && / manage$/) { print; } else { $in = 0; } } __DATA__ aaa23 fgfgdf pppp Released test bbb34 fgfgdf pppp Released test dfsad324 fgfgdf pppp Released test efdewr23 fgfgdf pppp Released dev dsarfew234 fgfgdf pppp skip dev dqewr2321 fgfgdf pppp skip prod sdsw32 fgfgdf pppp Released prod asdw234 fgfgdf pppp Released prod sadw2342 fgfgdf pppp Released qa deww234 fgfgdf pppp Released qa qdrqew234 fgfgdf pppp block qa swd234 fgfgdf pppp Released manage swdq234 fgfgdf pppp Released manage dfwfr43 fgfgdf pppp Released manage drqewr234 fgfgdf pppp Released manage drqewr234 fgfgdf pppp Released qb

Output:

15:54 >cd ..\Tests\1901-2000 15:54 >perl 1916_SoPW.pl efdewr23 fgfgdf pppp Released dev dsarfew234 fgfgdf pppp skip dev dqewr2321 fgfgdf pppp skip prod sdsw32 fgfgdf pppp Released prod asdw234 fgfgdf pppp Released prod sadw2342 fgfgdf pppp Released qa deww234 fgfgdf pppp Released qa qdrqew234 fgfgdf pppp block qa swd234 fgfgdf pppp Released manage swdq234 fgfgdf pppp Released manage dfwfr43 fgfgdf pppp Released manage drqewr234 fgfgdf pppp Released manage 15:54 >

Hope that helps,

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

Replies are listed 'Best First'.
Re^4: Reading multiple lines from text file
by newperlbie (Acolyte) on Aug 08, 2018 at 07:20 UTC
    Thank You....yes,it helped. Looking for a little variation now, if I need to output only the keys dev,prod,qa,manage and not the entire line? can a little variation here help? or even more,if I am using variables instead of hardcoding, say if (/ $first/../ $last/), how to get only these strings, print and store them into an array(?) which can be traversed later.

      Since the key is always the last word on the line, you can get it by using split (which defaults to splitting on whitespace) and then index the resulting list with [-1] to get the last word (if any). You can then push the key onto an array for later use:

      use strict; use warnings; use Data::Dump; my $first = 'dev'; my $last = 'manage'; my $in = 0; my @keys; while (<DATA>) { if (defined(my $key = (split)[-1])) { if ($key eq $first .. $key eq $last) { $in = 1; push @keys, $key; } elsif ($in && $key eq 'manage') { push @keys, $key; } else { $in = 0; } } } dd @keys; __DATA__ < as before >

      Output:

      17:42 >perl 1916_SoPW.pl ( "dev", "dev", "prod", "prod", "prod", "qa", "qa", "qa", "manage", "manage", "manage", "manage", ) 17:42 >

      Note: the defined test is needed only if the input file might contain blank lines.

      Hope that helps,

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

        Ah thanks!! but the key is not the last word on the line!! in fact that was my previous comment, but I removed it since I figured a way to grep it from the middle of the line.The key column can be any where ,not necessarily last....what should be the adaptation now.
        aaa23 fgfgdf test pppp Released bbb34 fgfgdf test pppp Released dfsad324 fgfgdf test pppp Released efdewr23 fgfgdf dev pppp Released dsarfew234 fgfgdf dev pppp skip dqewr2321 fgfgdf prod pppp skip sdsw32 fgfgdf prod pppp Released asdw234 fgfgdf prod pppp Released sadw2342 fgfgdf qa pppp Released deww234 fgfgdf qa pppp Released qdrqew234 fgfgdf qa pppp block swd234 fgfgdf manage pppp Released swdq234 fgfgdf manage pppp Released dfwfr43 fgfgdf manage pppp Released drqewr234 fgfgdf manage pppp Released

        2018-08-19 Athanasius added code tags