in reply to Regex question

TIMTOWTDI (and inelegant and verbose, but I think, 'linear and explicit'). Think in terms of removing 6 lines, rather than 5 and 1.

#!C:/perl/bin use strict; use warnings; # /me is too bleary at this hour to eliminate the "Usel +ess use of private variable in void context at ..." (lines 56 & 61) if (!$ARGV[0]) { print "Useage: perl 738855.pl String_to_search_for\n"; exit; } my $regex_string = $ARGV[0]; my $re = qr/$regex_string/; my @data_array; while (my $line = <DATA>) { chomp($line); push @data_array, $line; } my $flag = ''; my $test_line; my @cache; for $test_line(@data_array) { $test_line =~ /($re)/; if ($1) { $flag = "Found"; push @cache, "$flag"; next; } else { push @cache, $test_line } } # print Dumper @cache; my $i = 0; my $cache; for $cache(@cache) { if ( $cache =~ /Found/ ) { last; } else { $i++; } } # print "\$i: $i\n"; # $i = 6 when using the __DATA__ below my $j = ( $i - 5 ); # "-5": 5 preceding lines (and current + line) to delete for ($j; $j <= $i; ++$j ) { $cache[$j] = ''; } $j = $i; for ( $j; $j <= ($i+3); ++$j ) { # "$i+3" 3 following lines to disc +ard $cache[$j] = ''; } for $cache(@cache) { if ($cache) { print "$cache \n"; } else { next; } } __DATA__ Line 1 Line 2 should be removed line 3 should be removed line 4 should be removed line 5 should be removed line 6 should be removed line 7 should be removed this has more than 5 preceding lines and more + than 3 following lines. FOO line 8 should be removed line 9 should be removed line 10 should be removed line 11

Except for the warnings noted above, output is as desired:

Line 1 line 11
hth

Replies are listed 'Best First'.
Re^2: Regex question
by jamsda (Novice) on Jan 26, 2009 at 05:07 UTC

    Thanks for responses guys!

    Here's my first whack at it

    #!/usr/bin/perl use Getopt::Long; GetOptions( 'd=s' => \$delete, 'i=s' => \$patternmatch, 'c=s' => \$confile, ) open (FILE, $confile); while (<FILE>) { $line[$.] = $_; if (/$patternmatch/) { delete $line[$_] for ($.-5 .. $.); } } close(FILE); open (OUTPUT, ">$confile"); print (OUTPUT @line); close (OUTPUT);

    perl ./dellines.pl -i <patternmatch> -c ./someconfile -d

    I'm also trying to get the (-d) to be a stand-alone flag. What would be the best approach to this?

    Thanks, Jim

      That can delete too many lines.

      01 keep 02 keep <- gets discarded by your code. 03 keep <- gets discarded by your code. 04 keep <- gets discarded by your code. 05 keep <- gets discarded by your code. 06 keep <- gets discarded by your code. 07 discard B5 08 discard B4 09 discard B3 10 discard B2 11 discard B1 12 somepat 13 somepat

      That's in addition to the problems with $.-5 going negative.

      By the way, splice would be more efficient than delete since it wouldn't shrink the allocated buffer.

      I'm also trying to get the (-d) to be a stand-alone flag. What would be the best approach to this?

      'd!' => \$delete,

      -c ./someconfile

      That's weird. Why not just print to STDOUT and redirect output? It's way more flexible at no cost.

      perl dellines.pl -i somepat -d > someconfile

      The name of the option doesn't even make sense. It's an output file, not a config file as far as the tool is concerned.