#!/usr/bin/perl ################ # Jesse Lovegren Nov-2011, # update Mar-2012: added color=off switch, make single-spaced, # reset line counter between files. # update Jun-2012: added -i switch for case-insensitive search # No copyright asserted # to install, change the first line as necessary, # make executable and copy into # an accessible location, e.g. /usr/bin/ ################ use strict; use Term::ANSIColor; my $color = 1; my $caseS = 1; ################ if (not $ARGV[1]){print "mffind. Search over multiple text files\n", "=" x 50, "\n", "usage: mffind [file names] [search string]\n", qq(example: mffind "*tex" "[cC]ontrary"), "\n", "=" x 50, "\n", qq(Add switch --color=off or -i as last argument for no color or case insensitive), "\n";} my $fileDescriptor = shift; my $searchString = shift; my $flag = shift; if ($flag eq "--color=off"){$color = 0;} elsif ($flag eq "-i"){$caseS = 0;} ################ my @fileList = glob "$fileDescriptor"; my $fileIndex; ################ my $searchRegex; if ($caseS){$searchRegex = qr/$searchString/;} else {$searchRegex = qr/$searchString/i;} ############### for $fileIndex ( @fileList ){ open IF, "$fileIndex"; my $line; while ($line = ){ if ($line =~ m/$searchRegex/){ my $rightMatch = $'; chomp $rightMatch; if ($color){ print colored ['yellow'], "$fileIndex: ";} else {print "$fileIndex: ";} if ($color){print color 'green';} print "$."; if ($color){print color 'reset';} print " $`"; if ($color){print color 'red';} print "$&"; if ($color){print color 'reset';} print "$rightMatch\n"; } } close IF; }