I want to search for a line in a file, using regex, inside a Perl script.
Assuming it is in a system with grep installed, is it better to:
grep
through an open()
commandopen()
the file directly and use a while
loop and an if ($line =~ m/regex/)
?In a modern Perl implementation, the regexp code should be just as fast as in grep, but if you're concerned about performance, why don't you simply try it out? From a code cleanliness and robustness standpoint, calling an external command line tool is definitely not good.
You don't need to open
the file explicitly.
my $regex = qr/blah/;
while (<>) {
if (/$regex/) {
print;
exit;
}
}
print "Not found\n";
Since you seem concerned about performance, I let the match and print
use the default $_
provided by not assigning <>
to anything, which is marginally faster. In normal production code,
while (my $line = <>) {
if ($line =~ /$regex/) {
print $line;
exit;
}
}
would be preferred.
Edit: This assumes that the file to check is given on the command line, which I just noticed you had not stated applies in your case.
One thing to be careful of with grep: In recent Linux distributions, if your LANG environment variable defines a UTF-8 type (e.g. mine is LANG=en_GB.UTF-8) then grep, sed, sort and probably a bunch of other text-processing utilities run about 10 times more slowly. So watch out for that if you are doing performance comparisons. I alias my grep command now to:
LANG= LANGUAGE= /bin/grep
Edit: Actually, it's more like 100 times more slowly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With