Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

renaming files question

by Angharad (Pilgrim)
on Jan 06, 2006 at 13:09 UTC ( [id://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
1bn9.en.cnt
And other times like this
1bn9.pd.cnt
I want to circum-navigate this problem by renaming the file as follows
1bn9.cnt
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.
    -kulls

      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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-16 13:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found