Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Thanks for your help with my last post, it worked great. Now I am getting a little more complex and I need to make sure it works correctly or I may end up screwing up some of my data.

I have a folder tree of month names using the first 3 three letters (Jan, Feb, Mar). Inside each folder are text files, 1 for each day of the month. Titled 1.txt, 2.txt, 3.txt and so on and so on.

In my text file, I have the Month, Day, Year and Text. I want to automatically make a script go through my folder tree and input text where it should go (at the bottom of the correct Month/day file). A sample of the INPUT file is

Jan 1, 1915 (Birthday) Franck Pourcel - singer Jan 3, 1926 (Birthday) George Martin - singer Jan 3, 1945 (Birthday) Stephen Stills - singer Jan 3, 1946 (Birthday) John Paul Jones - singer
A sample of the file I am OUTPUTTING TO
1963 G Woods succeeds Eugene Black as president of the World Bank 1964 Federation of Rhodesia & Nyasaland dissolved 1965 International Cooperation Year begins
The output file only has the year, a space, and the TEXT. The other data from the INPUT file is used to locate the correct file.

This is what I have so far. I think it would work if the regex was matching, but it's not. Can anyone help me with this? I did the best I could.

#!/usr/bin/perl use warnings; use strict; open(LOG, "source.txt") or die "Error: $!"; my @lines = <LOG>; close(LOG); foreach my $line (@lines) { $line =~ m/(\w)\s+(\d+),\s+(\d+)\s+(\(Birthday\)\s.+)/i; # 1 is m +onth, 2 is day of month, 3 is year, 4 is the text print "$1 $2 $3 $4\n"; open(OUT, ">> $1/$2.txt") or die "Error"; # write our line appendi +ng to the bottom of the file print OUT "$3, $4\n"; } print "Done";

Replies are listed 'Best First'.
Re: Simple line regex again
by davidrw (Prior) on Jun 17, 2006 at 02:49 UTC
    I think it would work if the regex was matching, but it's not.
    what's the observed behavior?

    At a glance, i suspect that your first capture should be (\w+) instead of a single character.

    Also, you _must_ check if the pattern matched or not ..
    $line =~ m/ ... /i or next; $line =~ m/ ... /i or die "line '$line' failed to match"; $line =~ m/ ... /i or do { # log it somewhere next; };

    Probably also want to -d $1 || mkdir $1; to make sure the directory exists.
Re: Simple line regex again
by sulfericacid (Deacon) on Jun 17, 2006 at 02:46 UTC
    I'll take a stab at this. I did test this and it appears to work flawlessly.
    open(LOG, "source2.txt") or die "Error: $!"; my @lines = <LOG>; close(LOG); foreach my $line (@lines) { $line =~ m/(.+)\s+(\d+),\s+(\d+)\s+(\(Birthday\) .+)/; print "$1 $2 $3 $4\n"; open(OUT, ">> $1/$2.txt") or die "Error"; print OUT "\n$3 $4"; } print "Done";


    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid