#!/usr/bin/perl -w # http://www.linux.com/article.pl?sid=06/05/19/1920231 # perl version of grep. # # usage: pg [-iev] pattern [files...] use strict; use Getopt::Long; my %options; my @getopt_args = ( 'i', # case-independent 'e=s', # regular expression 'v', # print version ); Getopt::Long::config("noignorecase", "bundling"); usage() unless GetOptions(\%options, @getopt_args); version() if $options{'v'}; usage() if $options{'h'} || !@ARGV; # Case-independent search? my $regexp = $options{'e'} || shift(@ARGV); $regexp = '(?i)' . $regexp if $options{'i'}; # Reset line numbering on each input file so we can use $. while (<>) { next unless -f $ARGV; next unless (m/$regexp/og); print "$ARGV: $.: $_"; } continue { close ARGV if eof; # Not eof()! } exit(0); sub version { print q$Id: pg,v 1.1 2006/06/06 19:10:15 vogelke Exp $, "\n"; exit(0); } sub usage { die "usage: $0 [-eiv] pattern [file...]\n"; }