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

Re: [PBML] Re: Finding and parsing a substring

Expand Messages
  • H. Seibert
    please remove me from the mailing list? ... From: Charles Clarkson Sent: Monday, January 29, 2001 5:01 AM To: perl-beginner@yahoogroups.com Subject: [PBML] Re:
    Message 1 of 1 , Jan 29, 2001
    • 0 Attachment
      please remove me from the mailing list?

      ----- Original Message -----
      From: Charles Clarkson
      Sent: Monday, January 29, 2001 5:01 AM
      To: perl-beginner@yahoogroups.com
      Subject: [PBML] Re: Finding and parsing a substring


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

      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.