home *** CD-ROM | disk | FTP | other *** search
/ The UNIX CD Bookshelf / OREILLY_TUCB_UNIX_CD.iso / upt / examples / LINUX / ARCHIVE / PIPEGREP.Z / PIPEGREP / sbin / pipegrep
Encoding:
Text File  |  1993-01-01  |  984 b   |  58 lines

  1. #!///////////////////////////////////////////////////////////////////////////usr/STAGE/bin/perl
  2.  
  3. # Usage: pipegrep [\-l] pattern command [files]
  4.  
  5. if ($ARGV[0] eq '\-l') {
  6.     shift;
  7.     $action = <<'EOF';
  8.             print $file,"\n";
  9.             next file;
  10. EOF
  11. }
  12. else {
  13.     $action = <<'EOF';
  14.             print $file,":\t", $_;
  15. EOF
  16. }
  17.  
  18. # Get pattern and protect the delimiter we'll use.
  19.  
  20. $pat = shift;
  21. $pat =~ s/!/\\!/g;
  22.  
  23. # Get command and make sure it has a {}.
  24.  
  25. $cmd = shift;
  26. $cmd .= ' {}' unless $cmd =~ /{}/;
  27.  
  28. # Modify each filename into the corresponding command.
  29.  
  30. for (@ARGV) {
  31.     $file = $_;
  32.     $_ = $cmd;
  33.     s/{}/$file/;
  34.     s/$/ |/;
  35. }
  36.  
  37. # Generate the program.
  38.  
  39. $prog = <<EOF;
  40. file: foreach \$file (\@ARGV) {
  41.     open(FILE,\$file) || do {
  42.         print STDERR "Can't open \$file: \$!\\n";
  43.         next;
  44.     };
  45.     while (<FILE>) {
  46.         if (m!$pat!) {
  47.             $action
  48.         }
  49.     }
  50. }
  51. EOF
  52. print $prog if $debugging;
  53.  
  54. # And finally, do it.
  55.  
  56. eval $prog;
  57. die $@ if $@;
  58.