rainman_s
rainman_s

Reputation: 1108

Using rename tool to remove a character that is before an uppercase letter

I made a mistake in my script and replaced all spaces with 's' instead of underscore. I would like to undo this change. I have names like:

BatmansBegins
FightsClub
ThesDeparted
TouchsOfsEvil

I would like to name this to

Batman_Begins
Fight_Club
The_Departed
Touch_Of_Evil

I have this command at the moment but it replaces the 's' and the uppercase letter that follows it

rename -n 's/s[A-Z]/_/g'

Upvotes: 1

Views: 46

Answers (1)

anubhava
anubhava

Reputation: 785128

You need to capture uppercase letter and use a back-reference in replacement. So use it as:

rename -n 's/s([A-Z])/_$1/g' *s[A-Z]*

Another option is to use positive lookahead in your regex:

rename -n 's/s(?=[A-Z])/_/g' *s[A-Z]*

(?=[A-Z]) is positive lookahead that asserts presence of an uppercase letter after matching s.

PS: There are quite a few variants of rename tool and it appears that you're using popular perl based rename utility and this answer is also based on that.

Upvotes: 3

Related Questions