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

Oh Wise Ones,

I have a dir filled with files. Each file contains tab-delimited data that will ultimately be loaded into a database. The files each have a number as their suffix (ex. FILE.001, FILE.002, etc.) The data inside the files contain no reference as to which file the data came from. I wrote this little program that takes the filename and places it at the end of each line of data contained in the file. This works fine and solves the problem. The improvement I would like to make is instead of taking the entire filename I would like to take the suffix only (i.e. 001, 002, etc.). Looking at the code provided what would I have to add to just get the text after the period (.) in $file variable.

Thanks in advance for your help,

Eli
#!/usr/bin/perl # ingramid.pl use warnings; use strict; my @tempfile; my $file; my $ingramdir = "d:/ingram/ingramprc"; opendir(DIR, $ingramdir) or die "can't open $ingramdir: $!"; while (defined($file = readdir(DIR))) { print "$file\n"; open ingramoldfile, "d:/ingram/ingramprc/$file"; while (<ingramoldfile>){ chomp $_; push @tempfile, "$_\t$file\n"; } close ingramoldfile; } open ingramnew, ">d:/ingram/ingramprc/ingramall.txt"; print ingramnew @tempfile; close ingramnew; closedir(DIR);

Replies are listed 'Best First'.
Re: parse filenames
by lshatzer (Friar) on Apr 12, 2002 at 16:36 UTC
Re: parse filenames
by trs80 (Priest) on Apr 12, 2002 at 16:42 UTC
    You can use a regex of substr to do this.
    my ($numbers) = $file =~ m/(\d{3})$/;
    or
    my $numbers = substr ($file, -3, 3);
    The latter is much faster then the former, but personal preference may dictate which you decide to use.
      just to be on the safe side (in case the suffix contains letters or is longer than 3 chars):
      my $suffix = $file =~ m/\.(.+)$/;

      hope it helps,

        Make sure the file doesn't have more than one dot..

        my ($suffix) = $file =~ m/([^\.]+)$/;
Re: parse filenames
by snafu (Chaplain) on Apr 12, 2002 at 19:20 UTC
    I highly highly highly suggest going with lshatzer's suggestion and also read this node titled "Parse out the extension of a filename - return base of filename." written by yours truly. The portion of code to pay special attention to is by rob_au.

    This might be a really good node for you and indeed this subject has come up many times. If you will notice, I was trying to re-invent the wheel which many people like to try and do with this particular subject. I quickly learned, however, that I just invented a poorer implementation of the mouse-trap :(. I learned something though! :)

    The File::Basename module, imho, should be the only way to go for this task in a production script.

    _ _ _ _ _ _ _ _ _ _
    - Jim
    Insert clever comment here...

Re: parse filenames
by Anonymous Monk on Apr 13, 2002 at 00:37 UTC
    perl -pi.bak -e 's/$/qq(\t).substr$ARGV,-3/e' FILE*[0-9]
    Under Windows use " instead of '.