Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Pattern replace in a file name (updated)

by haukex (Archbishop)
on Mar 28, 2019 at 09:58 UTC ( #1231796=note: print w/replies, xml ) Need Help??


in reply to Pattern replace in a file name

There's More Than One Way To Do It... this one is cross-platform:

use warnings; use strict; use File::Spec::Functions qw/splitdir catdir/; while (my $line = <DATA>) { chomp $line; my $out = do { local $.=0; catdir grep {$.++;1../^\d+\.\d+$/} splitdir($line) }; print $out, "\n"; } __DATA__ /fd/gfree/tere/frf4545/geerg/fds/0.1/fsdf/dsakdsa/ /fd/gfree/tere/frf4545/geerg/dfds/5.9/fdsf/fdsfd/ /fd/gfree/tere/frf4545/geerg/dsad/02.44/fdsf/fdsf/

Output:

/fd/gfree/tere/frf4545/geerg/fds/0.1 /fd/gfree/tere/frf4545/geerg/dfds/5.9 /fd/gfree/tere/frf4545/geerg/dsad/02.44

Updates 1 and 2: Updated the code above to better limit the scope of the local $., and the map {...?$_:()} is better written as a grep.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2022-10-01 02:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (126 votes). Check out past polls.

    Notices?