#!/usr/bin/perl # egrep.pl - Simulate UNIX grep command. # Fedon Kadifeli, 1998 - April 2003. my $igncase = 0; my $nomatch = 0; my $onlyfilenames = 0; my $opt = shift; while ($opt =~ s/^\-//) { if ($opt =~ s/i//g) { $igncase = 1; } if ($opt =~ s/v//g) { $nomatch = 1; } if ($opt =~ s/l//g) { $onlyfilenames = 1; } $opt = shift; } $re = $opt; die "Usage: egrep.pl [-iv] regexp [files]\n" if ! defined $re; my @filelist; my $idx = 0; @ARGV = ("-") if $#ARGV < 0; for $fn1 (@ARGV) { for $fn (glob $fn1) { if (open FH, "<$fn") { $pref = $fn eq "-" ? "" : "$fn:"; my $found = 0; if ($igncase) { while () { if ((/$re/i) xor $nomatch) { $found = 1; if (!$onlyfilenames) { print "$pref$_"; } } } } else { while () { if ((/$re/) xor $nomatch) { $found = 1; if (!$onlyfilenames) { print "$pref$_"; } } } } if ($found && $onlyfilenames) { print $fn, "\n"; } close FH; } } }