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

Recently I posted a pattern matching question, which is repeated
below. I got many replies, thanks! Of course there are many way to do
this pattern matching, I'll just give the one I settled on.

--------------------------------------------------------------------
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.
--------------------------------------------------------------------

Here's a few of the suggestions:

***********
s/\d{2}[A-Z][a-z]{2}/$file_ddmon/;
************

***********
$file="abc21Augyyy";
$file_ddmon = "24Sep";
$file=~s/\d+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/$file_ddmon/;
***********

***********
Does it always appear before the .?  Ie <name><date>.<extension>?  If
so, try this:

s/\d+...\./$file_ddmon./;
$file_ddmon = $_;

Ie replace a sequence of digits followed by thcharacters followed by a
dot with the date and a dot.
***********

Here's what I actually used:

            $new_file =~ s/\d\d[A-Z][a-z]{2,2}/$file_ddmon/;

Which, as you can see, is substantially the same as the first
suggestion, except that it demands exactly two digits and exactly two
lowercase letters.

If any other beginner out there has question, please email, I'd be
happy to try and answer them. Thanks again to all who replied.

--


                                        (301) 286-2396



Sat, 17 Feb 1996 17:50:57 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. 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