I want to be able to take an argument from the command line and use it as a regular expression within my script to filter lines from my file. A simple example
$ perl script.pl id_4
In script.pl:
...
my $exp = shift;
while(my $line = <$fh){
if($line =~ /$exp/){
print $line,"\n";
}
}
...
My actual script is a bit more complicated and does other manipulations to the line to extract information and produce a different output. My problem is that I have situations where I want to filter out every line that contains “id_4” instead of only select lines containing “id_4”. Normally this could be achieved by
if($line !~ /$exp/)
but, if possible, I don’t want to alter my script to accept a more complex set of arguments (e.g. use !~ if second parameter is “ne”, and =~ if not).
Can anyone think of a regex that I can use (beside a long “id_1|id_2|id_3|id_5…”) to filter out lines containing one particular value out of many possibilities? I fear I’m asking for the daft here, and should probably just stick to the sensible and accept a further argument :/.
Why choose? Have both.
Usage: