in reply to Re: Regex question
in thread Regex question

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

Replies are listed 'Best First'.
Re^3: Regex question
by ikegami (Patriarch) on Jan 26, 2009 at 05:16 UTC

    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.