Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl regex stop looking in file after first match

Status
Not open for further replies.
Jun 3, 2007
84
US
Hello,

Wondering if someone could please help me out. I am pretty new with Perl and still learning. I am having problem with the following script shown below. The problem is that I don't want it to print out the same filename if more than one match is found...Meaning if the regex matches once then stop and move onto the next file. What it is doing right now for example is that if the log contains "abc.exe" "def.exe" "test.exe". Then I get a print out showing 080501.bk.dc01 three times. What do I need to add/change so that the script stops looking in the file after one match?

thanks for the help in advance.

Code:
#!/usr/bin/perl
use File::Find;
use MIME::Parser;

find(\&edits, $DIRECTORY);

sub edits() {
    if ( -f and /^log\..*dc01$/ ) {  
        my %seen;
        open LOG, "< $_" or die "Could not open $File::Find::name\n$!\n"; 

        foreach my $each_line (<LOG>) {
            if ( $each_line =~ /(filename=)\".+\.exe\"/xi ) { 
                print "$File::Find::name\n";
            }
        }
    }
}
 
Just terminate the foreach loop with a 'last'.
Code:
            if ( $each_line =~ /(filename=)\".+\.exe\"/xi ) {
                print "$File::Find::name\n";
                [b]last;[/b]
            }
 
Do you really want to stop searching in the file or just avoid displaying duplicate entries?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
This is a bit besides the point but I notice you are not closing your filhandle.
Isn't this causing trouble..?!?

I would assume that, since the file handle is global, something will start complaining when the routines is called the second time...

Anyway, it pays to be tidy, or so they say..
Code:
  ....
  foreach my $each_line (<LOG>) {
    if ( $each_line =~ /(filename=)\".+\.exe\"/xi ) {
      print "$File::Find::name\n";
      last;
    }
  }
  close(LOG);
 
Just avoid display the results but continue searching through the file.

thanks guys
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top