Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

renaming files question

by Angharad (Pilgrim)
on Jan 06, 2006 at 13:09 UTC ( #521466=perlquestion: print w/replies, xml ) Need Help??

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

Hi there
I have a perl script that creates a particular file. However, depending on the eventual users of the script, sometimes the filename might look like this
And other times like this
I want to circum-navigate this problem by renaming the file as follows
before processing it further
I've tried a simple mv command
mv 1bn9.*.cnt 1bn9.cnt
But that complains about their being multiple files and therefore the destination having to be a directory, which is not what I want at all.
Can anyone suggest a solution ... be it Perl or a Unix command? It would be much appreciated.

Replies are listed 'Best First'.
Re: renaming files question
by Happy-the-monk (Canon) on Jan 06, 2006 at 13:25 UTC

    If you don't care if there is more than one file with such a "middle extension" and want to run the risk of losing the others, this simple trick does it:

    perl -wle 'for ( <1bn9.*.cnt> ) { qx(mv $_ 1bn9.cnt) }'

    On the safe side, run it only once:

    perl -wle '$_ = <1bn9.*.cnt>; qx(mv $_ 1bn9.cnt) if $_'

    Cheers, Sören

Re: renaming files question
by tirwhan (Abbot) on Jan 06, 2006 at 13:19 UTC

    So I guess you only want to handle one of these files, regardless of how many there are? This should do the trick:

    my $a_file = (glob("1bn9.*.cnt")) or die "No file found"; rename $a_file,"1bn9.cnt" or die "Couldn't move file";

    There are ten types of people: those that understand binary and those that don't.
Re: renaming files question
by DungeonKeeper (Novice) on Jan 06, 2006 at 13:56 UTC
    It will only complain about multiple files if there are multiple files, in which case you don't want to collapse them into a single file before processing, so perhaps this is what you want:
    for my $original ( glob '/path/lbn9*.cnt' ) { rename $original, '/path/lbn9.cnt'; # file processing here... }
Re: renaming files question
by sh1tn (Priest) on Jan 06, 2006 at 13:46 UTC
    perl -e 'map{ /(1bn.)\..+\.(cnt)/ and rename $_, "$1.$2" }glob "1bn*"'

Re: renaming files question
by kulls (Hermit) on Jan 06, 2006 at 14:37 UTC
    try with the 'cut' command like
    `echo 1bn9.pd.cnt | cut -d. f1`
    to get the first column and
    `echo 1bn9.pd.cnt | cut -d. f3`
    to get the last column and merge it of both.

      If you're going to do this in shell, then you can avoid a bit of work:

      $ echo lbn9.pd.cnt | cut -d. -f1,3 lbn9.cnt
      However, since I think you're advocating writing the above in perl (with the backticks), it seems kind of redundant:
      join '.', (split '.', $name)[0,2]
      which avoids all the overhead of forking and execing multiple (2-3) subprocesses. Personally, I find the backticks (or even system in general) to be vastly overused in perl, mostly by people pretending it's the shell rather than using it as perl.

Re: renaming files question
by vek (Prior) on Jan 06, 2006 at 19:13 UTC

    Will multiple users be running the original script concurrently? If not, you could modify it to just append to lbn9.cnt and you wouldn't have to rename anything.

    -- vek --

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://521466]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2022-05-19 23:08 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (72 votes). Check out past polls.