1

I would like to rename files that have the following format:

somethingblabla15T06:58_31+0000somethingblabla.bla

THe new name should be

somethingblabla15T06:58:31+0000somethingblabla.bla

So the _ within the numbers should be replaced with :.

I would like to do this with the perl based rename command line tool.

I thought this would be a part of the regex:

's/[0-9](_)[0-9]/:'

I can't get it to work. I think this should also be escaped or so.

2 Answers 2

2

Try:

rename 's/([0-9])_([0-9])/$1:$2/'

Example

Consider a directory with this file:

$ ls som*
somethingblabla15T06:58_31+0000somethingblabla.bla

Let's run rename and then run ls again:

$ rename 's/([0-9])_([0-9])/$1:$2/' s*
$ ls som*
somethingblabla15T06:58:31+0000somethingblabla.bla

How it works

([0-9])_([0-9]) matches a digit followed by an underscore followed by a digit. Because of the parens, each digit is saved in a group. The replacement text is $1:$2 where $1 refers to the first saved group and $2 to the second saved group.

1

Another way to do the job without capture groups:

rename 's/(?<=\d)_(?=\d)/:/' something*

Explanation:

s/              # substitute
    (?<=\d)       # positive lookbehind, make sure we have a digit before
    _             # underscore
    (?=\d)        # positive lookahead, make sure we have a digit after
/               # with
    :             # colon
/               # end subst.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .