Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: RE question; how to insert a digit between const[a-z]*3 && inc[0-9]*3?

by kcott (Archbishop)
on Apr 16, 2013 at 11:35 UTC ( [id://1028879]=note: print w/replies, xml ) Need Help??


in reply to RE question; how to insert a digit between const[a-z]*3 && inc[0-9]*3?

G'day taint,

Here's another way to do it:

$ perl -Mstrict -Mwarnings -E ' my @files = qw{abc1.html abc12.html abc123.html abc1234.html}; say for map { s/^(abc)(\d{1,3})(\.html)$/$1 . "0" x (4 - length $2) . $2 . $ +3/e; $_ } @files; ' abc0001.html abc0012.html abc0123.html abc1234.html

Update: "... the files begin with the same 3 alpha characters ..." so \w{3} is far too generic: s/\w{3}/abc/

-- Ken

Replies are listed 'Best First'.
Re^2: RE question; how to insert a digit between const[a-z]*3 && inc[0-9]*3?
by taint (Chaplain) on Apr 16, 2013 at 17:53 UTC
    Greetings Ken, and thanks for the reply.
    This looks like a complete solution!
    Given that all of the files end in .html, and begin with abc
    I should be able to simply read in the entire directory.
    opendir(DIR, $dirname) or die "can't opendir $dirname: $!"; while (defined($file = readdir(DIR))) { # use your suggestion on "$dirname/$file" } closedir(DIR);
    I'm still sorting it all out. But wanted to take the time to thank you
    for your help.

    Thanks again!

    --chris

    #!/usr/bin/perl -Tw
    use perl::always;
    my $perl_version = "5.12.4";
    print $perl_version;

      Given that context, you might want to try something like this technique (untested):

      while (defined($file = readdir(DIR))) { next unless $file =~ /^(abc)(\d{1,3})(\.html)$/; rename "$dirname/$file" "$dirname/" . $1 . "0" x (4 - length $2) . + $2 . $3; }

      I'd also recommend that you try this with a print to ensure you're in the right directory and targetting the correct files before making thousands of changes; then change to rename. Some error checking/handling might also be useful.

      -- Ken

        Greetings Ken, and thanks again!
        Your example(s) both work exactly as you indicated they would.
        Took me awhile to sort it all out. But I'm afraid I wasn't as clear, or as concise
        as I could/should have been -- soory. :(
        I had already performed a mas-rename on the files by cobbling a sh(1) script, with the
        help of find(1), basename(1), cp(1), && rm(1) through a for loop.
        This is what left me with the task of correcting the references to the files previous
        names within the newly named files. This is where my request came in -- again, sorry for not better clarifying.
        I tried to apply your logic to the task of reading the contents of the files
        and applying it to their contents. But I guess I'm just not yet proficient enough in perl(1). :(
        So I got anxious, and mostly conquered the abc100 - abc999.html's
        via another sh(1) script
        #!/bin/sh - # fixem -- a simple stupid && inefficient shell script for name in $(find . -type f -name '*.html' -maxdepth 1) do # sed(1) recognizing Extended RE(s) sed -Ef fixem.sed <$name >temp.txt mv temp.txt $name done rm -f temp.txt
        fixem.sed
        /abc([1-9][0-9][0-9])\.html/s/abc/abc0/g
        While that shell script fixed most of the occurrences, it missed a few, and it
        also didn't address abc1 - 99.html. So now I'm going to try and figure out how
        to best attack/accomplish this same (il)logic in perl(1). :)

        Any pointers/guidance/solutions still greatly appreciated. :)

        Thanks again for everyones' time, and consideration.

        --chris

        #!/usr/bin/perl -Tw
        use perl::always;
        my $perl_version = "5.12.4";
        print $perl_version;

Log In?
Username:
Password:

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

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

    No recent polls found