#!/usr/bin/perl # crude perl script to find a piece of text ($search_text) # in files of a certain filename format ($file_glob) # in a specified directory ($start_dir) or any subdirectory below that # $case_sens chose case sensitive or insensitive search # $verbose sets level of output # $verbose = 0 gives total count only # $verbose = 1 gives count per file (if any in the file, with filename) # $verbose = 2 gives linenumbers in which text appears # $verbose = 3 gives lines in which the text appears and linenumber # Don't think I wrote the core of this, don't know where I found it originally! # Chris Evans (C.Evans@sghms.ac.uk) -- June 1996 # ###### SET SEARCH PARAMETERS HERE ######## $search_txt = "ukcp"; $start_dir = "."; $file_glob = "*.htm"; $case_sens = 0; # 1 for case sensitive, 0 for insensitive $verbose = 3; # 3 gets full line printed, # 2 gets line numbers # 1 gets file summary info, # 0 gets summary only # ###### you shouldn't need to change anything below here ####### if ($case_sens) { print "Finding \"$search_txt\" in files like $file_glob in or below $start_dir\n\n"; } else { print "Case insensitive search for \"$search_txt\" in files like $file_glob in or below $start_dir\n\n"; } $totflag = $flag = 0; open(FIND, "find $start_dir -name \"$file_glob\" -print |") || die "Couldn't run find: $!\n"; print "\n\nNo files matching $file_glob found in or below $start_dir\n\n" if (!) ; FILE: while ($filename = ) { chop $filename; next FILE unless -T $filename; if (!open(TEXTFILE, $filename)) { print STDERR "Can't open $filename -- continuing ...\n"; next FILE; } $flag = 0; $lineno = 0; while () { $lineno++; if ($case_sens == 1) { if (/$search_txt/) { if ((!$flag) && ($verbose >= 2)) { print "\nFile: $filename:\n"; } if ((!$flag) && ($verbose == 2)) { print "Linenumbers: $lineno "; } elsif ($verbose == 2) { print "$lineno "; } $flag++; if ($verbose == 3) { print "Occurrence $flag in $filename at line num $lineno:\n$_"; } } } else { if (/$search_txt/i) { if ((!$flag) && ($verbose >= 2)) { print "\nFile: $filename:\n"; } if ((!$flag) && ($verbose == 2)) { print "Linenumbers: $lineno "; } elsif ($verbose == 2) { print "$lineno "; } $flag++; if ($verbose == 3) { print "Occurrence $flag in $filename at line num $lineno:\n$_"; } } } } $totflag += $flag; if (($verbose >= 1) && ($flag >= 1)) { print "\n" if ($verbose = 2); if ($flag == 1) { print "$search_txt found once in $filename \n"; if ($verbose == 3) { print "\n\n"; } } else { print "$flag occurrences of $search_txt found in $filename \n"; print "\n\n" if ($verbose == 3); } } } if (!$totflag) { print "\n\"$search_txt\" not found in files matching $file_glob below $start_dir\n"; } elsif ($totflag == 1) { print "\n\"$search_txt\" found once in files matching $file_glob below $start_dir\n"; } else { print "\n\"$search_txt\" found $totflag times in files matching $file_glob below $start_dir\n"; }