duficy
asked on
search for pattern in list of files and print out filename of matches
I'm trying to write perl script that will search through all my domains and print out the file names of the ones with matching IP address. All my files names equal the domain name. So I'm trying to search for a certain pattern and print out the file names that match. I was hoping to search through all the files and PUSH the matching file names into @domains.
What am I doing wrong. I'm new to programming and perl. (You probably can tell)
#!/usr/bin/perl
# this will search for domains with certain IP's address and print out the filename
use strict;
my $current = "";
my @domains = "";
my $pattern = <STDIN>;
#while (<>) {
# if ($current ne $ARGV) {
# $current = $ARGV;
print "Enter search pattern for domains\n";
$pattern = <STDIN>;
chomp ($pattern);
if (/$pattern/) {
print "found domain\n";
}
else {
print "domains not found \n";
}
push @domains, $_;
~
~
What am I doing wrong. I'm new to programming and perl. (You probably can tell)
#!/usr/bin/perl
# this will search for domains with certain IP's address and print out the filename
use strict;
my $current = "";
my @domains = "";
my $pattern = <STDIN>;
#while (<>) {
# if ($current ne $ARGV) {
# $current = $ARGV;
print "Enter search pattern for domains\n";
$pattern = <STDIN>;
chomp ($pattern);
if (/$pattern/) {
print "found domain\n";
}
else {
print "domains not found \n";
}
push @domains, $_;
~
~
ASKER
I need to search within the file. It appears that your program searches on the file name. Example I have numerous DNS files, each file name equals the domain name. Example one file might be called experts-exchange.com. The contents of the file would include:
www IN A 206.169.61.246
I need to search for a numeric string in the file and print out the names of the files that contain the matched string.
www IN A 206.169.61.246
I need to search for a numeric string in the file and print out the names of the files that contain the matched string.
> I was hoping to search through all the
files and PUSH the matching file names into @domains.
#!/usr/bin/perl -w
use strict;
use File::Find;
2==@ARGV or die "Usage: $0 pattern directory\n";
my @domains;
find sub { /$ARGV[0]/o && push @domains, $File::Find::name }, $ARGV[1];
# do something with @domains here
files and PUSH the matching file names into @domains.
#!/usr/bin/perl -w
use strict;
use File::Find;
2==@ARGV or die "Usage: $0 pattern directory\n";
my @domains;
find sub { /$ARGV[0]/o && push @domains, $File::Find::name }, $ARGV[1];
# do something with @domains here
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
duficy,
Welcome back!!!
You have the following 4 questions open for a long time now. Please take some time to close these questions ASAP.
EE userid duficy
Total questions asked 19 (100%)
Open questions 4
Topic Area URL Date
Linux Administration https://www.experts-exchange.com/jsp/qShow.jsp?ta=linuxadmin&qid=20188928 10/01/01
Linux Networking https://www.experts-exchange.com/jsp/qShow.jsp?ta=linuxnet&qid=20160811 07/31/01
Linux https://www.experts-exchange.com/jsp/qShow.jsp?ta=linux&qid=11785998 11/07/00
Windows NT Networking https://www.experts-exchange.com/jsp/qShow.jsp?ta=winntnet&qid=11570378 10/16/00
Your help in closing these questions will be highly appreciated.
Thanks,
maneshr
(NOT a moderator at EE)
Welcome back!!!
You have the following 4 questions open for a long time now. Please take some time to close these questions ASAP.
EE userid duficy
Total questions asked 19 (100%)
Open questions 4
Topic Area URL Date
Linux Administration https://www.experts-exchange.com/jsp/qShow.jsp?ta=linuxadmin&qid=20188928 10/01/01
Linux Networking https://www.experts-exchange.com/jsp/qShow.jsp?ta=linuxnet&qid=20160811 07/31/01
Linux https://www.experts-exchange.com/jsp/qShow.jsp?ta=linux&qid=11785998 11/07/00
Windows NT Networking https://www.experts-exchange.com/jsp/qShow.jsp?ta=winntnet&qid=11570378 10/16/00
Your help in closing these questions will be highly appreciated.
Thanks,
maneshr
(NOT a moderator at EE)
ASKER
Damn that's a cool program. It works great. Now I need some time to understand how it works.
Thanks again.
Thanks again.
# this will search for domains with certain IP's address and print out the filename
use strict;
print "Enter search pattern for domains\n";
my $pattern = <STDIN>;
chomp ($pattern);
my @domains = grep/$pattern/,@ARGV;
if( @domains ){
print "found domain @domains\n";
}else{
print "domains not found \n";
}