#!/usr/bin/perl use warnings; use strict; # use diagnostics; #regexp.pl # 911425 print "Enter the full path to the directory you want to search: "; my $folder = <>; chomp $folder; chdir($folder); opendir(DIR, $folder) or die "Can't open $folder, $!"; my @files = readdir(DIR) or die "Can't readdir $folder, $!"; print "What's the phrase you're looking for?: "; my $find = ; chomp $find; my $searchterm = qr/$find/; my (%found, $found, $file); for $file(@files) { next if ($file =~ /^\./); next unless (-T $file); # text files only (excludes binary files such as *.doc or .xls) open(my $fh, '<', $file) or die "Can't open $file: $!"; my @content = <$fh>; for my $line(@content) { if ($line =~ /$searchterm/i) { my $key = $file; $found{$key} += 1; } } } while (my ($key, $value) = each %found) { print "$key has \t $value instance(s) of \t \"$find\"\n"; }