Loading ...
Sorry, an error occurred while loading the content.

Re: Finding and parsing a substring

Expand Messages
  • Don Smith
    Charles, Thanks for the script and the speedy response. I ran into a bug the first time I ran it, but fixed it by adding a semi-colon at line 24 and now the
    Message 1 of 3 , Jan 29, 2001
    • 0 Attachment
      Charles,
       
      Thanks for the script and the speedy response.  I ran into a bug the first time I ran it, but fixed it by
      adding a semi-colon at line 24 and now the script works perfectly.  Thank you.
       
      Your help is very much appreciated.
       
      Kindest regards,
       
      Don Smith
       
       
       
      From: Charles Clarkson
      Sent: Monday, January 29, 2001 5:01 AM
      Subject: [PBML] Re: Finding and parsing a substring

      Don,

         I cobbled this together from answer I gave in another
      group. It's not fancy, but it should get you started.

      HTH,
      Charles K. Clarkson


      #!/usr/bin/perl -w
      use strict;
      use diagnostics;

      # I'm trying to find a PERL script that will read a bunch
      # of text files in a specific directory, look for a
      # substring that varies in each file and write the
      # substring to a text file (all on a Windows machine.)

      # The substring is an identifier that begins with
      #       "PID" followed by 24 digits(no spaces & no decimals).
      # The regular expression would be:
      #     PID\d{24}
      # The \d tells perl to look for a digit. The {24} tells perl to
      #      look for 24 of the previous character.

      my $directory = qw/ c:\perl\ /;
      my $write_file = qw/ out.txt /;

      use File::Find;

      open (LOG, ">$write_file") || die "Cannot open $write_file $!";
            find(\&wanted, $directory);
      close LOG

      sub wanted {
            # $File::Find::name contains the complete pathname to the file
            # next if directory
            return if -d $File::Find::name;

            # $_ contains just the current filename
            my $filename = $_;
            open (FILEHANDLE, "<$File::Find::name")
                 || die "Cannot open $File::Find::name $!";
                  # This loop assumes there is only 1 PID per line.
                  while (<FILEHANDLE>) {
                        # goto next line unless we get a match
                        next unless /(PID\d{24})/;
                        # $1 contains everything between () above
                        print LOG "$1\n";
                  }
            close FILEHANDLE;
      }








      Get your FREE download of MSN Explorer at http://explorer.msn.com

    Your message has been successfully submitted and would be delivered to recipients shortly.