I am new to perl and want to emulate grep -n like this:
want:
# egrep -n 'malloc\(|free\(|printf\(' test.c
5:p = malloc(sizeof(char));
6:printf("Test\n");
7:free(p);
have:
# perl grep.pl test.c
malloc\(line 7
free\(line 7
printf(
Processed 10 lines
Script:
#!/usr/bin/perl
$verbose = 1;
@pattern = ('malloc\(', 'free\(', 'printf(');
$counter = 0;
open(FH, "<", $ARGV[1]) or die;
while (<>) {
my $matches = (@pattern[0-2]);
$counter++;
# print "line $counter:$_" if ($_ =~ /malloc\(/o);
print join("line $counter\t\n",@pattern),"\n" if ($_ =~ /$matches/o);
close (FH);
}
print "\n";
$verbose == 1 && print "Processed $counter lines\n";
Somehow the counter is wrong. What am I missing here?