Super User Asked by linux_user_11880 on January 14, 2021
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.
Try:
rename 's/([0-9])_([0-9])/$1:$2/'
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
([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.
Correct answer by John1024 on January 14, 2021
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.
Answered by Toto on January 14, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP