CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
expanding the search for patterns in a file to include a list of patterns

 



regex2012
User

Oct 14, 2015, 9:32 AM

Post #1 of 4 (958 views)
expanding the search for patterns in a file to include a list of patterns Can't Post

expanding the idea of printing out lines that contain variable:

I now have this:
The $file_contents contains an entire list of patterns, which I verified by using print $file_contents
However, I can't get anything to show in the output of this script, even though /tmp/listfile.txt contains some of the patterns- not sure what I am doing wrong.


Code
open FILE, "</tmp/bplist.txt"; 
$file_contents = do { local $/; <FILE> };

open my $fh, '<', '/tmp/listfiles.txt' or die "unable to open file '$file' for reading : $!";
while (<$fh>) {
push @lines, $_ if /$file_contents/;
print "$_\n" foreach @lines;
}



FishMonger
Veteran / Moderator

Oct 14, 2015, 9:46 AM

Post #2 of 4 (956 views)
Re: [regex2012] expanding the search for patterns in a file to include a list of patterns [In reply to] Can't Post

Is the list of patterns on a single line separated by the | character, or are they on separate lines?


(This post was edited by FishMonger on Oct 14, 2015, 9:46 AM)


regex2012
User

Oct 14, 2015, 9:56 AM

Post #3 of 4 (953 views)
Re: [FishMonger] expanding the search for patterns in a file to include a list of patterns [In reply to] Can't Post

They are on separate lines, here is a real simple example that is what I am talking about:

Code
/tmp/bplist (File with patterns) 
dog
cat
train
car
bat
boxes
files
folders
windows
....etc



FishMonger
Veteran / Moderator

Oct 14, 2015, 10:15 AM

Post #4 of 4 (950 views)
Re: [regex2012] expanding the search for patterns in a file to include a list of patterns [In reply to] Can't Post


Code
open my $bplist_fh, '<', '/tmp/bplist.txt' or die $!; 
chomp(my @words = <$bplist_fh>);
close $bplist_fh;

my $pattern = join '|', @words;
my @lines;
open my $fh, '<', '/tmp/listfiles.txt' or die "unable to open file '$file' for reading : $!";
while (<$fh>) {
push @lines, $_ if /$pattern/;
}
print "$_\n" foreach @lines;


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives