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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to search a file again once a regex is matched.

Status
Not open for further replies.
Jun 3, 2007
84
US
Hello hoping someone could help me. I am trying to create a script which searches through all files in a directory for a string, if the string is found I want it to look through the entire file which it found the first string and look for a second string. Right now I have the code below, but it looks like when it tries to match the second string it's comparing the regex against the result/output of the first regex not against the entire file which it found the fist regex in.

How can I make my script search for a string against a file if it finds the string in that file then search that entire file once again for the second string.

thanks for the help.

Code:
sub edits() {
    if ( -f and /^Khlog.08$/ ) {
        foreach ( $File::Find::name ) {
             open(LOG, "< $File::Find::name") or die "Could not open file $_: $!";
             while ( <LOG> ) {
                   my $LINE = $_;
                   if ( $LINE =~ m/002389983/ ) {
	            print "Looking for: 002389983\n Found it in file: $LINE\n";
                        if ( $LINE =~ m/29994339499/ ) {
	                 print "Looking for: 002389983\n Found it in file: $LINE\n";
		}
		else {
		      print "Could not find the second string: 002389983\n";
                        }
                   }
             }
      }
}
 
If the first $LINE matches, the second match is impossible.
Try reading the entire file into an array and then run the first match. If the first match occurs then try matching the whole array against the second match.

Keith
 
maybe (untested) :

Code:
sub edits {
   if ( (-f) && /^Khlog\.08$/ ) {
   my $FLAG = 0;
   open(LOG, "< $File::Find::name") or die "Could not open file $File::Find::name: $!";
   while ( my $LINE = <LOG> ) {
      if ( $LINE =~ m/002389983/ ) {
         print "Looking for: 002389983\n Found it in file: $LINE";
         $FLAG = 1;
         while ( $LINE = <LOG> ) {
            if ( $LINE =~ m/29994339499/ ) {
               print "Looking for: 002389983\n Found it in file: $LINE\n";
               $FLAG = 2;
            }
         }
      }
   }
   if ($FLAG == 1) {
      print "Found 002389983 only\n";
   }
   elsif ($FLAG == 2) {
      print "Found 00238993 and 29994339499\n";
   }
   else {
      print "Nothing found\n";
   }
}

assumes 299943339499 comes after 00238993 in the file

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hey Kevin that works perfectly. I am not quite sure I follow the logic though, if you get a chance can you please explain? What exactly is it that FLAG is doing? It looks like it resets to look through the file again, kinda like seek I am guessing?

Thanks again for your expertise :)
 
The flag does not reset anything, and as I said, it assumes the one pattern always comes after the other in the file so there is never a need to start searching the file from the beginning again. If there is a need to start from the beginning add seek as noted above by prex1.

The flag is just used to indicate what was matched. 0 indicates no match, 1 indicates the first pattern only was matched and 2 indicates both patterns were matched.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top