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

I would like to use regex to remove underscores from multiple files name.

But i encountered a problem

For eg I came across two files name as follows


file1 = Hello_World

file2 = Hello_World_

I would like to remove the last underscore in file2 to get Hello_world but i would like to keep file1 as it is. How do i do so?

i have tried s/_/$1/ but it doesn't works.

Replies are listed 'Best First'.
Re: Regex help
by Zaxo (Archbishop) on May 22, 2005 at 22:46 UTC

    s/_$//; The dollar anchor associates underscore with end of string.

    After Compline,
    Zaxo

Re: Regex help
by ww (Archbishop) on May 23, 2005 at 01:20 UTC
    I realize this is sort of a 'duh'
    but before you do this, most file systems won't allow two files of the same name in the same directory.

    Depending on your OS, the name change will fail... or worse, even fubar your directory, so... if you're talking about actually changing the names in storage (rather than changing, say -- names in a list of names, think ahead.

Re: Regex help
by sweetblood (Prior) on May 22, 2005 at 23:44 UTC
    checkout perldoc -f tr

    $file =~ tr /_//d;

    HTH Sometimes I just don't read! Sorry for the confusion.

    Sweetblood