regex - how to extract titles from a text file and use those titles to open other files and search for patterns -
i trying figure out perl program reads text file containing file names, opens each of files , searches them regular expression (eg ggggg).
i reasoning first need read file , save array.
then foreach
element of array, open
corresponding file , search within it.
can please help?
this code works (gratia chankey pathak) lines containing 1 title not needing processing:
$filename = 'names.txt'; open (my $fh, "<", $filename) or die $!; while ( <$fh> ) { chomp $_; $file_contents; { open (my $fh, '<', $_) or die $!; $file_contents = ''; while (<$fh>) { $file_contents .= $_; print "matched!" if $file_contents =~ /gggggg/i; } } }
but if name file full of names (few/line) separated \t?
my approach below solve problem.
my $filename = 'names.txt'; open (my $fh, "<", $filename) or die $!; # assuming each line contains file name while ( <$fh> ) { chomp $_; $file_contents; { open (my $fh, '<', $_) or die $!; local $/ = undef; $file_contents = <$fh>; close $fh; } print "matched!" if $file_contents =~ /ggggg/; }
see:
Comments
Post a Comment