Tricky pattern matching question - (at least I think it's tricky) 
Author Message
 Tricky pattern matching question - (at least I think it's tricky)

I'm just a beginner at perl, so this one is a little hard for me, but
I shouldn't wonder if it would be easy for many of you. Here's the
problem: I have a list of file names, each filename has a date
embedded in it, and the file itself has the date embedded in it too.
When everything is kosher, those two dates should match, but sometimes
they don't, in which case I want to change the date in the name to
match the date in the file. For example, here's three file names:

uab22Aug.12z
uabf22Aug.00z
uabf22Aug.12z

I have a C program that will go into any one of these files and return
the date the file actually applies. My perl script takes this date and
compares it to the date in the file name. So, for example, if the date
in the file uab22Aug.12z was 21Aug, my perl script would detect that
and now I want a way to rename that file to uab21Aug.12z

Here's the code that does the checking:

# Now we'll check the dates for all the office note 84 format files.

    if (&wrong_date(split(/\s+/,`/home/ejh/nmc_link/check84 $_`))){
        print "Date in file $_ is $file_ddmon.\n";
        print "Renaming and moving file to it's proper place...\n";

    }

Quote:
}

sub wrong_date {

    $file_ddmon = $day . $month_name[$month-1];
    if ($file_ddmon ne $ddmon) {
        return $file_ddmon;
    }

Quote:
}

So my question is this: given the current file name (uab22Aug.12z) in
$_, and the correct date string (21Aug) in $file_ddmon, how would I
take the rest of the file name (uabXXXXX.12z) and stick in the string
$file_ddmon (in place of the XXXXX)? Remember that there are not
always three letters before the date in the filename (eg. there's
file uabf22Aug.12z).

Would I do this with some kind of pattern substitution? Or is there
some other better way?

Any advice or suggestions would be helpful.

--


                                        (301) 286-2396



Sat, 10 Feb 1996 00:26:16 GMT  
 Tricky pattern matching question - (at least I think it's tricky)

:
:I'm just a beginner at perl, so this one is a little hard for me, but
:I shouldn't wonder if it would be easy for many of you. Here's the
:problem: I have a list of file names, each filename has a date
:embedded in it, and the file itself has the date embedded in it too.
:When everything is kosher, those two dates should match, but sometimes
:they don't, in which case I want to change the date in the name to
:match the date in the file. For example, here's three file names:
:
:uab22Aug.12z
:uabf22Aug.00z
:uabf22Aug.12z
:
:I have a C program that will go into any one of these files and return
:the date the file actually applies. My perl script takes this date and
:compares it to the date in the file name. So, for example, if the date
:in the file uab22Aug.12z was 21Aug, my perl script would detect that
:and now I want a way to rename that file to uab21Aug.12z
:
:Here's the code that does the checking:
:
:# Now we'll check the dates for all the office note 84 format files.

:    if (&wrong_date(split(/\s+/,`/home/ejh/nmc_link/check84 $_`))){
:        print "Date in file $_ is $file_ddmon.\n";
:        print "Renaming and moving file to it's proper place...\n";
:        
:    }
:}
:
:
:sub wrong_date {

:    $file_ddmon = $day . $month_name[$month-1];
:    if ($file_ddmon ne $ddmon) {
:        return $file_ddmon;
:    }
:}
:
:
:So my question is this: given the current file name (uab22Aug.12z) in
:$_, and the correct date string (21Aug) in $file_ddmon, how would I
:take the rest of the file name (uabXXXXX.12z) and stick in the string
:$file_ddmon (in place of the XXXXX)? Remember that there are not
:always three letters before the date in the filename (eg. there's
:file uabf22Aug.12z).
:
:
:Would I do this with some kind of pattern substitution? Or is there
:some other better way?

I'd do something like this.  Note that I'm getting my dates from the
files' modification times, but you appear to be getting them from a file.
So you might want to change how to get the $internal date.  But certainly
don't exec a program each time to do it: write a function to parse the
file yourself, preferably caching it somewhere so you only read once.


    sub mkdate {
        local($this_day,$this_mon) = (localtime(shift))[3,4];
        sprintf("%02d%s", $this_day, $month[$this_mon]);
    }

    $wallclock = &mkdate(time);


        next unless $file =~ /^...(\d\d[a-z]{3})\./i;
        $external = $1;
        $internal = &mkdate((stat($file))[9]);
        next if $internal eq $external;
        ($newname = $file) =~ s/$external/$internal/;
        print "changing $file to $newname\n" if $debug;
        rename($file, $newname) || warn "$0: can't rename $file to $newname: $!\n";
    }

--tom
--



Wed, 14 Feb 1996 22:00:14 GMT  
 Tricky pattern matching question - (at least I think it's tricky)
Oops.  He said NOT always three letters before the filename.  So change        

    next unless $file =~ /^...(\d\d[a-z]{3})\./i;

to

    next unless $file =~ /^\D+(\d\d[a-z]{3})\./i;

--tom
--



Wed, 14 Feb 1996 22:01:46 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. SUMMARY: Tricky pattern matching question - (at least I think it's tricky)

2. tricky pattern match

3. Tricky Listbox question (at least for me!)

4. beginner's question (on pattern matching, I think)

5. Tricky: Generate matching string from regexp

6. Tricky question on calling a shell from a perl script

7. Tricky (?) looping syntax questions

8. Tricky #! Question

9. Tricky column formatting/space removing question

10. tricky (?) find an replace question

11. tricky Question

12. Some (tricky?) regexp questions

 

 
Powered by phpBB® Forum Software