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

How do you remove a metacharacter from a file name. I have a filename of cos.NEW&OLD.0826.12345 but the perl script only recognizes cos.NEW. The NEW&OLD is a EDI Trading Partner ID. I want to remove the ampersand and replace it with an underline so that the code does not generate a false alert. I tried $script=~s/(\W)/_/g; but is that to replace what is in the contents of the file because I am still getting the error message.

08/26/2009_16:16:36 14802 ***ERROR: cannot upload data file /usr/edi/cos.NEW&OLD.0826.14802 into Database 08/26/2009_16:16:36 14802 *** ALERT 521

Snippet of code below

$script=$ARGV[3]; # Value of this $ARGV is NEW&OLD : : $otis_db="$base_dir/otis_db/cos.$script.$cdate.$pid"; : : : : chomp($end_date=`date +%m/%d/%Y_%T`); $end_date=~s/_/ /g; $values="(\'$file_name\',\'$send_from_dir\',\'$serv_ref\',TO_D +ATE\(\'$end_date\',\'MM\/DD\/YYYY HH24:MI:SS\'))\;"; : I placed this line right before the echo statement. Was getting an er +ror could not upload " cos.NEW&OLD.082609.12345" because it could not + be found. But it did create the file "cos.NEW". The & and anything + after is ignored. $script=~s/(\W)/_/g; : `echo "INSERT into COSBRIDGE\n\tvalues$values\n" >> $otis_db`;

Replies are listed 'Best First'.
Re: Removing a Metacharacter from a filename
by bv (Friar) on Aug 26, 2009 at 21:43 UTC

    Any reason you are using backticks and echo when you could open the file for appending?

    open my $OTIS, '>>', $otis_db or die "Can't open $otis_db: $!"; print $OTIS "INSERT into COSBRIDGE\n\tvalues$values\n"; close $OTIS

    Because it appears it's your shell that has the problem with the &.

    $,=' ';$\=',';$_=[qw,Just another Perl hacker,];print@$_;

      Thanks for the reply bv. Actually, I didn't write the original code. I was assigned a quick fix task for removing the ampersand symbol. I would have thought replacing the symbol with an underline would elimate the false error alert. But, it didn't work as I am still getting the Error Message. It does label the filename as "cos.NEW" as is evident in the log file: *** OTIS data uploaded /usr/edi/cos.NEW Wed Aug 26 16:16:36 EDT 2009 *** 1 row created. I guess I am confused on why the scan and replace did not work.

        Well, if you still need to use the scan-and-replace method, you need to move it up before this line:

        $otis_db="$base_dir/otis_db/cos.$script.$cdate.$pid";

        Since after that, any changes to $script do not affect the $otis_db variable, which is what gets used to create the file.

        $,=' ';$\=',';$_=[qw,Just another Perl hacker,];print@$_;
Re: Removing a Metacharacter from a filename
by biohisham (Priest) on Aug 26, 2009 at 22:27 UTC
    When you tried:

    $script=~s/(\W)/_/g;
    you're actually replacing every none word character with an underscore. hence your filename "cos.NEW&OLD.0826.12345" would become "cos_NEW_OLD_0826_12345" which is obviously cumbersome to write, just in case. If you want everything remain the way it is, just escape the metacharacter itself.
    my $script = "cos.NEW&OLD.0826.12345"; $script=~s/\&/_/; print "$script"; #outputs "cos.NEW_OLD.0826.12345"

    NOTE:Regular expression are not as consternating as they may seem!, invest the time at learning them effectively and you would see it coming your way easier than the head-whirlpool I used to suffer from when I did not know nothing about regexes, Now at least I can analyse and correlate them better. See it for yourself :)


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

      Thanks for your reply biohisham. The one line of code I entered did not work. It didn't replace anything as I still am getting the error message. I am a little bit at a loss.

        I tested it, it gave me the same output in my previous reply, I will update it to work without an escape character since that conforms more because the ampersand is not considered one of the metacharacters in Perl.

        However, I am sure the problem can be somewhere else, just tidy this a little bit and give us your code, more descriptively, and list these error messages, I am sure someone here would get to solve it provided it that you stated your problem in the proper light.


        Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.