user3105274
user3105274

Reputation: 11

sed - insert a "_" between [a-z][a-z][A-Z][a-z] >>> [a-z][a-z]_[A-Z][a-z]

I have filenames and made a failure in my sed script so I get:

"this_is_the_videoIphone.mp4"
"this_is_my_videoNikon.mp4"
"this_is_the_pictureIphone.mp4"
"this_VideoIphone.flv" ... and so on

I want a "_" between that.

a rule could be like this: (one or more [a-z]) followed by ([A-Z] followed by one or more [a-z])

I tried much but nothing works...

filename="$(echo $filename | sed -e 's/\(^.*[a-z]\)\([A-Z][a-z].*$\)/\1_\2/g')"

What is wrong?

Upvotes: 1

Views: 83

Answers (2)

potong
potong

Reputation: 58391

This might work for you (GNU sed):

sed 's/\B[[:upper:]]\B/_&/g' file

This inserts a _ preceeding any uppercase character within a word.

N.B. This does not conform to your specification in that it would insert a _ between two consecutive uppercase characters. Nor would it insert said character if the last character of the word was uppercase.

Upvotes: 0

Cyrus
Cyrus

Reputation: 88583

I suggest with GNU sed:

sed -E 's/([a-z][a-z])([A-Z][a-z])/\1_\2/'

Upvotes: 1

Related Questions