in reply to Re: Perl Possibilities
in thread Perl Possibilities
And "search_script.pl" would be something like this:cd {top_level_directory_where_all_files_are_located} find * -type f | search_script.pl > hit_list.txt
Note that in bash you can redirect STDERR as well:#!/usr/bin/perl use strict; use warnings; while (<STDIN>) { chomp; next unless -f(); my $text = do { if ( open( my $fh, $_ )) { local $/; <$fh>; } else { warn "Unable to read $_\n"; } }; if ( $text =~ /\s(recommend\S*\s+to\s+vote\s+\S+)/ ) { ( my $hit = $1 ) =~ s/\s+/ /g; print "$_: $hit\n"; } else { print "$_: NO_MATCH\n"; } }
The output to STDOUT will tell you which files have the sought-for text, and what the text was. It also lists the files that failed to match, so you can take a closer look at those, and tweak the regex as needed.find * -type f | search_script.pl > hit_list.txt 2> search.errlog
The regex proposed above will match all the inflections on "recommend" (-ed, -ing, -s, -ation), and will capture the matched phrase only up to the word that follows "vote". (You can extend the capture to include more words before and/or after, if you like, by adding more \s+\S+\s+ elements inside the parens.)
When there's a match, all kinds of white-space between words is allowed, and it's all normalized to a single space before output, to ensure one line of output per file.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Perl Possibilities
by Gideau (Novice) on Mar 16, 2016 at 12:49 UTC | |
by graff (Chancellor) on Mar 18, 2016 at 22:05 UTC |