#!/usr/bin/perl use Getopt::Std; sub HELP_MESSAGE { print "pgrep [-Hhinv] pattern [file...]\n"; print "-H: with filename\n"; print "-h: without filename\n"; print "-n: with line number\n"; print "-i: case insensitive\n"; print "-v: invert\n"; exit 1; } sub VERSION_MESSAGE { print "chri's pgrep, version 1.0\n"; } getopts('nhHiv'); HELP_MESSAGE unless @ARGV; $pattern = shift @ARGV; $with_filename = (@ARGV > 1); $with_filename = 1 if $opt_H; $with_filename = 0 if $opt_h; $with_number = $opt_n; while(<>) { my $match; if($opt_i) { $match = /$pattern/i; } else { $match = /$pattern/; } if($opt_v) { next if $match; } else { next unless $match; } print "$ARGV: " if $with_filename; print "$.: " if $with_number; print; }