Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: rename and regex

  1. #1
    Join Date
    Dec 2007
    Beans
    373

    rename and regex

    How can you batch rename files while retaining certain portions and replacing others? For example:

    original filename:
    prefix.Y01Z18.suffix.ext

    renamed to:
    y01z18.ext

    All files have this same format but: the suffix is not always the same text; the prefix is not always the same; the numbers after Y and Z change from file to file.

    What I am looking for is something along the lines of

    rename 's/.+YxxZxx.+\.ext/yxxzxx\.ext/' *.ext

    where xx leaves that digit intact. I know about a program but that is overkill for what I want and it actually adds more than I want to the filenames.

    Thanks...
    Last edited by rebeltaz; May 27th, 2013 at 10:10 PM. Reason: removed prohibited content

  2. #2
    Join Date
    Apr 2012
    Beans
    5,529

    Re: help with rename and regex?

    You can do that by grouping the bits you want to keep between parentheses, and then substituting those groups using $1, $2, etc. in the replacement string e.g.

    Code:
    $ rename -nv 's/.*[.]S([0-9]+)E([0-9]+)[.].*[.]mp4/s$1e$2.mp4/' *.mp4
    prefix.S01E18.suffix.mp4 renamed as s01e18.mp4
    Last edited by papibe; May 27th, 2013 at 10:37 PM. Reason: consistency with OP's edit

  3. #3
    Join Date
    Dec 2007
    Beans
    373

    Re: help with rename and regex?

    Thank you SO much! That is exactly what I was looking for!

  4. #4
    Join Date
    Dec 2007
    Beans
    373

    Re: help with rename and regex?

    One thing.. what does the [] around the dots do? Thanks.

  5. #5
    Join Date
    Apr 2012
    Beans
    5,529

    Re: help with rename and regex?

    In general [ ] defines a range of characters for example later we have [0-9] meaning any single digit, but in the case of [.] it's just a way to escape the regex meaning of '.' (= any single character) so that it matches a literal period instead - an alternative way to do the same thing is with a backslash escape ie \. but sometimes [.] is easier to read

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

    Re: help with rename and regex?

    '[.]' is the same as '\.' - it means match one of the characters inside (or one not inside) the square brackets, ie [xyzX] will match any of x, y, x, or X, while [^xyzX] will match any that is NOT x, y, z, or X. as usual you can use *+? etc to specify repetition.

    anyway you do this because '.' means 'any character'

    edit: a better way would be
    Code:
    rename -n 's|.*([a-zA-Z][0-9][0-9][a-zA-Z][0-9][0-9]\.[a-zA-Z0-9]*)|\L$1|' fileOrFiles
    which will match any of
    Code:
    blahblah.S11E11.ext
    blahy32Z32.extension
    really long filename.with.extra.stuff.A12B33.extension
    and turn them into
    Code:
    s11e11.ext
    y32z32.extension
    a12b33.extension

    Last edited by HiImTye; May 28th, 2013 at 12:50 AM.

  7. #7
    Join Date
    Dec 2007
    Beans
    373

    Re: help with rename and regex?

    Oh, OK.. that was what confused me. I am used to seeing it as \.

    Thanks again!

  8. #8
    Join Date
    Dec 2007
    Beans
    373

    Re: help with rename and regex?

    Quote Originally Posted by HiImTye View Post
    edit: a better way would be
    Code:
    rename -n 's|.*([a-zA-Z][0-9][0-9][a-zA-Z][0-9][0-9]\.[a-zA-Z0-9])|\L$1|' fileOrFiles
    which will match any of
    Code:
    blahblah.S11E11.ext
    blahy32Z32.extension
    really long filename.with.extra.stuff.A12B33.extension
    and turn them into
    Code:
    s11e11.ext
    y32z32.extension
    a12b33.extension
    OK... I guess '|' is taking the place of '/'. It looks like that just extracts the a12b34 pattern and carries it to the other side along with the extension? I like that. What does the 'L' in 'L$1' do?

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

    Re: rename and regex

    \L converts everything that follows to lowercase, while \l converts the next letter to lowercase. I'd share a good info page but I need to set up Firefox Sync on my tablet again

    and yeah, I like pipes, it prevents picket fencing

    edit: I edited my previous post so thst it grabs more than one letter extensions /braindead
    Last edited by HiImTye; May 28th, 2013 at 12:51 AM.

  10. #10
    Join Date
    Dec 2007
    Beans
    373

    Re: rename and regex

    Quote Originally Posted by HiImTye View Post
    \L converts everything that follows to lowercase, while \l converts the next letter to lowercase. I'd share a good info page but I need to set up Firefox Sync on my tablet again

    and yeah, I like pipes, it prevents picket fencing

    edit: I edited my previous post so thst it grabs more than one letter extensions /braindead

    I just now got around to trying this and it is grabbing everything after the first dot after the A12B33.

    e.g.
    really.long.filename.with.extra.stuff.A12B33.and.m ore.stuff.extension

    becomes

    a12b33.and.more.stuff.extension

    instead of

    a12b33.extension


    I have been trying to figure out on my own how to match the characters after the last dot, but nothing is working..

Page 1 of 3 123 LastLast

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
  •