开发者

help - sed - insert space between any string of form XxxXxx, without replacing the pattern

开发者 https://www.devze.com 2023-02-01 04:58 出处:网络
If the string is of the pattern XxxXyzAbc... The expected out put from sed has to be Xxx Xyz Abc ... eg: if the string is QcfEfQfs, then the expec开发者_如何学Goted output is Qcf Ef Efs.

If the string is of the pattern XxxXyzAbc...

The expected out put from sed has to be Xxx Xyz Abc ...

eg: if the string is QcfEfQfs, then the expec开发者_如何学Goted output is Qcf Ef Efs.

If i try to substitute the pattern [a-z][A-Z] with space then the sed will replace the character or pattern with space, like Qc f fs.

Is there any way to insert space in between without replacing the pattern ?.

Kindly help. Thanks.


Use match groups.

$ sed 's/\([a-z]\)\([A-Z]\)/\1 \2/g' <<< 'XxxXyzAbc'
Xxx Xyz Abc


A more accurate pattern match - one with fewer spurious matches on strings that are not in the XxxYyyZzz format - is:

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

Consider the inputs:

XxxYyyZzz
xxxYyyZzz
X xY yZ z

One other answer I see will insert spaces in all the strings - when it should only modify the first string.

Updated to allow for the QcfEfEfs example - it isn't clear from the question how many lower case letters in a row are allowed. It could be 1 or 2; or it could be 1 or more - using a * allows for 1 or more; otherwise, use \{1,2\} for 1 or 2.


This might work for you (GNU sed):

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

Looks for a non-word boundary followed by an uppercase character and inserts a space throughout the line.

0

精彩评论

暂无评论...
验证码 换一张
取 消