Help - sed - insert a space between any line of the XxxXxx form without replacing the template

If the string has an XxxXyzAbc pattern ...
The expected result from sed should be Xxx Xyz Abc ...

for example: if the string is QcfEfQfs , then the expected result is Qcf Ef Efs.

If I try to replace the pattern [az] [AZ] with a space, sed will replace the character or pattern with a space, for example Qc f fs.

Is there any way to insert space between them without replacing the template ?.

Kindly help. Thank you

+4
source share
3 answers

Use match groups.

$ sed 's/\([az]\)\([AZ]\)/\1 \2/g' <<< 'XxxXyzAbc' Xxx Xyz Abc 
+6
source

A more accurate pattern match — one with fewer false matches of strings that are not in the XxxYyyZzz format — is:

 sed 's/\([AZ][az][az]*\)\([AZ][az][az]*\)\([AZ][az][az]*\)/\1 \2 \3/' 

Consider the inputs:

 XxxYyyZzz xxxYyyZzz X xY yZ z 

Another answer that I see will insert spaces in all lines - when it should only change the first line.

Updated to allow QcfEfEfs example - it is not clear from the question how many valid letters per line are valid. It can be 1 or 2; or it can be 1 or more - use * allows 1 or more; otherwise use \{1,2\} for 1 or 2.

0
source

This may work for you (GNU sed):

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

Searches for a string without words, followed by an uppercase character, and inserts a space over the entire string.

0
source

Source: https://habr.com/ru/post/1332994/


All Articles