Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

search for pattern in list of files and print out filename of matches

Posted on 2002-03-13
6
Medium Priority
?
215 Views
Last Modified: 2010-03-05
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, $_;
~
~
0
Comment
Question by:duficy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 6862450
#!/usr/bin/perl
# 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";
}
0
 

Author Comment

by:duficy
ID: 6863109
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.
0
 
LVL 5

Expert Comment

by:Sapa
ID: 6863112
> 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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Accepted Solution

by:
Sapa earned 200 total points
ID: 6863133
> 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.

This program already exists. It's 'grep'. But if you want the same functionality in perl:

#!/usr/bin/perl -w
use strict;
use File::Find;

2==@ARGV or die "Usage: $0 pattern directory\n";
my @domains;
sub wanted {
  if (open F, "< $_") {
    my $occured;
    while (defined(my $l = <F>)) {
      $occured++, last if $l =~ /$ARGV[0]/o;
    }
    close F;
    push @domains, $File::Find::name if $occured;
  }
}

find \&wanted, $ARGV[1];
# do something with @domains here
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6863302
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   http://www.experts-exchange.com/jsp/qShow.jsp?ta=linuxadmin&qid=20188928   10/01/01  
Linux Networking   http://www.experts-exchange.com/jsp/qShow.jsp?ta=linuxnet&qid=20160811   07/31/01  
Linux   http://www.experts-exchange.com/jsp/qShow.jsp?ta=linux&qid=11785998   11/07/00  
Windows NT Networking   http://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)
0
 

Author Comment

by:duficy
ID: 6864608
Damn that's a cool program. It works great. Now I need some time to understand how it works.

Thanks again.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question