[comp.sources.d] fast grep for perl with enhanced r.e.

bob@dhw68k.cts.com (Bob Best) (08/07/88)

This perl script for grep(1) actually executed faster than the /bin/grep
on my SCO Xenix 386 system (case insensitive searches were slower.)
In addition, all the enhancements that Larry Wall added to Henry Spencer's
regular expression package are available.
See the perl manual page under "Regular Expressions" for details.
-------------cut here-------------------
# grep.pl - perl version of grep(1)
# written by bob best (bob@dhw68k.cts.com)
#
$match=1;
while ($_ = $ARGV[0], /^-/) {
	shift;
	/^-y/ && ($fold='i') && next;
	/^-v/ && ($match=0) && next;
	/^-c/ && ($count=1) && next;
	/^-n/ && ($line=1) && next;
	/^-l/ && ($fileonly=1) && next;
}
$pattern=$ARGV[0]; shift;
++$multifile if $#ARGV > 0;

dogrep: {

if ($match && $fileonly) {
	eval "while (<>) { if (/$pattern/$fold) {
		print \"\$ARGV\\n\"; close(ARGV);} } ";		
	last dogrep;
}

if ($match && $count && $multifile) {
	eval "while (<>) { ++\$cnt if /$pattern/$fold;
		if (eof) { print \"\$ARGV:\$cnt\\n\"; \$cnt=0;} } ";
	last dogrep;
}

if ($match && $count) {
	eval "while (<>) { ++\$cnt if /$pattern/$fold;} print \"\$cnt\\n\"; ";
	last dogrep;
}

if ($match && $multifile && $line) {
	eval "while (<>) { print \"\$ARGV:\$.:\$_\" if /$pattern/$fold;
		close(ARGV) if eof; } ";
	last dogrep;
}

if ($match && $multifile) {
	eval "while (<>) { print \"\$ARGV:\$_\" if /$pattern/$fold; } ";
	last dogrep;
}

if ($match && $line) {
	eval "while (<>) { print \"\$.:\$_\" if /$pattern/$fold;} ";
	last dogrep;
}

if ($match) {
	eval "while (<>) { print if /$pattern/$fold; } ";
	last dogrep;
}

if (!$match && $fileonly) {
	eval "while (<>) { unless (/$pattern/$fold) {
		print \"\$ARGV\\n\"; close(ARGV);} } ";		
	last dogrep;
}

if (!$match && $count && $multifile) {
	eval "while (<>) { ++\$cnt unless /$pattern/$fold;
		if (eof) { print \"\$ARGV:\$cnt\\n\"; \$cnt=0;} } ";
	last dogrep;
}

if (!$match && $count) {
	eval "while (<>) { ++\$cnt unless /$pattern/$fold;} print \"\$cnt\\n\"; ";
	last dogrep;
}

if (!$match && $multifile && $line) {
	eval "while (<>) { print \"\$ARGV:\$.:\$_\" unless /$pattern/$fold;
		close(ARGV) if eof; } ";
	last dogrep;
}

if (!$match && $multifile) {
	eval "while (<>) { print \"\$ARGV:\$_\" unless /$pattern/$fold; } ";
	last dogrep;
}

if (!$match && $line) {
	eval "while (<>) { print \"\$.:\$_\" unless /$pattern/$fold;} ";
	last dogrep;
}

if (!$match) {
	eval "while (<>) { print unless /$pattern/$fold; } ";
	last dogrep;
}

} #end of dogrep block
-- 
Bob Best
uucp: ...{trwrb,hplabs}!felix!dhw68k!bob	InterNet: bob@dhw68k.cts.com