#!/usr/bin/perl # # recursive string grepper -lynx98 require 'find.pl'; $File::Find::dont_use_nlink = 'DONT!'; require 'getopt.pl'; &Getopt; $grep = shift or die < [] > options: -v = verbose -i = ignore case -e = edit each matching file -r = match-string is a regular expression -b = also grep binaries -H = skip files that end in .html? -s = suppress access errors -c = count matches instead of printing matching lines X $|=1; my @list; &find($#ARGV >= 0 ? @ARGV : '.'); print STDERR '=' x 78, "\n" if $opt_v; if ($opt_e) { $EDITOR = $ENV{EDITOR} || 'vi'; print STDERR "calling '$EDITOR +/$grep' on ", join(" ", @list), "\n"; system $EDITOR, "+/$grep", @list; } exit; sub wanted { ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime) = lstat; return $prune = 1 if /^CVS$/; return if -d _ or -l _; return if $size < length($grep); return if /^.#/ or /~$/ or /\.fmt$/; # fmt, special case for psyced return if $opt_H and /\.html?$/i; return unless $opt_b or -T _; if ($opt_v) { $len = length $name; print STDERR $name, ' ', '=' x (77-$len), "\r" if $len < 77; } if (open(I, $_)) { @file = split("\n", &slurp(*I)); @matches = grep($opt_r ? ($opt_i ? /$grep/oi : /$grep/o) : ($opt_i ? /\Q$grep\E/oi : /\Q$grep\E/o), @file); close I; } else { print STDERR "cannot access $name\n" unless $opt_s; } if (@matches) { if ($opt_c) { print $name, " : ", 1 + $#matches, " \n"; } else { print $name, "\n", join("\n", @matches), "\n\n"; } push @list, $name; # $diff = `diff $flags $name $oth/$v`; } } # reads a file from a stream into a variable all at once # sub slurp { local(*IN) = @_; local($save) = $/; undef $/; local($data) = ; $/ = $save; return $data; }