Increment Numbers

Discussion in 'Perl Misc' started by Chris Ellison, Jan 2, 2006.

  1. Hi,


    "Larry's filename fixer"

    ------filename: rename-------------
    #!/usr/bin/perl -w
    # rename - Larry's filename fixer
    $op = shift or die "Usage: rename expr [files]\n";
    chomp(@ARGV = <STDIN>) unless @ARGV;
    for (@ARGV) {
    $was = $_;
    eval $op;
    die $@ if $@;
    rename($was,$_) unless $was eq $_;
    }
    ----------------------------------

    I have a bunch of files 001.txt, 002.txt, 055.txt, 099.txt,... and
    would like to increment them using the above script.

    So, I was thinking something like this:

    rename 's/(\d{3})/something/' *

    Though, I don't know how to accomplish the "something".
    I was thinking that it could be done with backreferencing:

    $1+increment

    but I don't know how to do operations within a regular expression.

    Any suggestions?
    Chris Ellison, Jan 2, 2006
    #1
    1. Advertising

  2. Chris Ellison

    Xicheng Jia Guest

    you may use sprintf() and regex's 'e' modifier:

    rename 's/^(\d{3})/sprintf("%03d",$1+1)/e' *.txt

    Xicheng

    Chris Ellison wrote:
    > Hi,
    >
    >
    > "Larry's filename fixer"
    >
    > ------filename: rename-------------
    > #!/usr/bin/perl -w
    > # rename - Larry's filename fixer
    > $op = shift or die "Usage: rename expr [files]\n";
    > chomp(@ARGV = <STDIN>) unless @ARGV;
    > for (@ARGV) {
    > $was = $_;
    > eval $op;
    > die $@ if $@;
    > rename($was,$_) unless $was eq $_;
    > }
    > ----------------------------------
    >
    > I have a bunch of files 001.txt, 002.txt, 055.txt, 099.txt,... and
    > would like to increment them using the above script.
    >
    > So, I was thinking something like this:
    >
    > rename 's/(\d{3})/something/' *
    >
    > Though, I don't know how to accomplish the "something".
    > I was thinking that it could be done with backreferencing:
    >
    > $1+increment
    >
    > but I don't know how to do operations within a regular expression.
    >
    > Any suggestions?
    Xicheng Jia, Jan 2, 2006
    #2
    1. Advertising

  3. Xicheng Jia <> wrote:

    > you may use sprintf() and regex's 'e' modifier:



    There is no such thing as an 'e' modifier for a regex.


    > rename 's/^(\d{3})/sprintf("%03d",$1+1)/e' *.txt



    Oh. You meant the _substitution operator's_ 'e' modifier.



    [snip TOFU]


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 2, 2006
    #3
  4. Chris Ellison <> wrote:

    > rename 's/(\d{3})/something/' *



    > but I don't know how to do operations within a regular expression.



    That's OK because you don't want to do an operation within
    a regular expression, you want to do an operation within
    the REPLACEMENT part of the s/// operator (which is (by default)
    a string, not a regex).


    > Any suggestions?



    Read the entry for s/// in perlop.pod, until you get to this part:

    e Evaluate the right side as an expression.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 2, 2006
    #4
  5. Chris Ellison

    Dave Weaver Guest

    <TOFU fixed>

    On 1 Jan 2006 19:23:36 -0800, Xicheng Jia <> wrote:
    > Chris Ellison wrote:
    > > I have a bunch of files 001.txt, 002.txt, 055.txt, 099.txt,... and
    > > would like to increment them using the above script.

    >
    > you may use sprintf() and regex's 'e' modifier:
    >
    > rename 's/^(\d{3})/sprintf("%03d",$1+1)/e' *.txt
    >


    But note that this will probably fail, depending on the order the
    files are processed in.

    If you start with 001.txt, and 002.txt, and it first renames 001.txt
    to 002.txt, and then renames 002.txt to 003.txt, you'll find you've
    lost a file and 003.txt is what was originally 001.txt.

    You may be better off writing a small custom script to do the renaming
    correctly.
    Dave Weaver, Jan 6, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    104
    Views:
    10,927
    Jordan Abel
    Oct 28, 2005
  2. Replies:
    99
    Views:
    2,474
    eliza81
    Jun 11, 2010
  3. Subra
    Replies:
    25
    Views:
    1,169
    user923005
    Mar 8, 2007
  4. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    879
    Alf P. Steinbach /Usenet
    May 22, 2011
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    504
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page