Results 1 to 3 of 3

Thread: perform math while renaming

  1. #1
    Join Date
    May 2009
    Location
    Courtenay, BC, Canada
    Beans
    1,661

    perform math while renaming

    I'm fairly new to using perl and was wondering if there was a way to perform math on the perl string while renaming bulk files. here is how I would normally rename something:
    Code:
    rename -n 's/0(\d{2})/1$1/' *
    which would, for instance, perform the following on my dummy files:
    Code:
    tye@T:~/Documents/you're a dummy$ rename -n 's/0(\d{2})/1$1/' *
    dummy001 renamed as dummy101
    dummy002 renamed as dummy102
    dummy003 renamed as dummy103
    I would like to be able to, for instance, subtract 1 or more from the stored string so that dummy002 could come out as dummy001, etc..
    Last edited by HiImTye; February 13th, 2012 at 01:54 PM.

  2. #2
    Join Date
    Dec 2009
    Beans
    167

    Re: perform math while renaming

    Take a look at 'e' (evaluate) modifier, which makes possible the code execution in the substitution part. For your example try something along the lines of:

    Code:
    rename -n 's/(\d+)/ sprintf("%.3d", $1-1) /e' dummy*

  3. #3
    Join Date
    May 2009
    Location
    Courtenay, BC, Canada
    Beans
    1,661

    Re: perform math while renaming

    Quote Originally Posted by erind View Post
    Take a look at 'e' (evaluate) modifier, which makes possible the code execution in the substitution part. For your example try something along the lines of:

    Code:
    rename -n 's/(\d+)/ sprintf("%.3d", $1-1) /e' dummy*
    that actually solved two questions I had, the other was how to pass variable length numbers to prename

    thanks a million!

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •