tejas1991 has asked for the wisdom of the Perl Monks concerning the following question:
# problem with this script is, i want to check all the lines of all the files included in the diretory and if the pattern is not found then only print that string not found, # but instead it is printing on every line string not found . and if the string is found i.e matched in a file the it should discontinue its search . Please help me folks#!/usr/bin/perl my $file_name = $ARGV[0]; # parsing filename the elements of thi +s file will be sotred in @store array my $dir = $ARGV[1]; # parsing the directory in which the c +ontents of array will be searched in each file of this directory open (F, "$file_name") || die ("Could not open $file_name!"); # open +ing file while($line = <F>) { print "pushwala while"; push (@store, $line); #storing contents of file to @stor +e array } print "@store"; # printing contents of array close (F); # closing file opendir (DIR, $dir) or die $!; # opening directory , of which we ha +ve to precess all files my @dir = readdir DIR; foreach my $filename (@dir) { # print "$filename\n"; open (F, "$dir/$filename") || die ("Could not open $filename"); # o +pening Each file of the directory foreach (@store) { print "$_"; # printing contents of @store array , F +IXME here is the problem this prints 3 times all the contents of @sto +re array while($line = <F>) # cheking each line of files in th +e directory { print "$line"; # check if ($line =~ /$_/) # pattern matching between co +ntens of array and each line of files in directory { chomp ($_); #last if; print "testcase name:$_\n"; + # print "file includeing testcase:$filename\n"; + # if pattern is found printing it print "line:$line\n"; + # break; # breaking while loop } else { chomp ($_); print "$_:not found\n\n"; + # if pattern is not found break; # breaking while loop } } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Search strings stored in array into files of a directory
by Athanasius (Cardinal) on Jul 27, 2015 at 13:33 UTC | |
|
Re: Search strings stored in array into files of a directory
by GotToBTru (Prior) on Jul 27, 2015 at 12:14 UTC | |
|
Re: Search strings stored in array into files of a directory
by toolic (Bishop) on Jul 27, 2015 at 11:20 UTC | |
|
Re: Search strings stored in array into files of a directory
by Nemo Clericus (Beadle) on Jul 27, 2015 at 12:55 UTC | |
|
Re: Search strings stored in array into files of a directory
by pme (Monsignor) on Jul 27, 2015 at 13:25 UTC | |
|
Re: Search strings stored in array into files of a directory
by 1nickt (Canon) on Jul 27, 2015 at 14:23 UTC | |
|
Re: Search strings stored in array into files of a directory
by soonix (Chancellor) on Jul 28, 2015 at 09:09 UTC |