I have a file containing the following
String, SomeotherString Additional, StringNew String
I would like to have the following output:
String, Someother
String Additional, String
New String
The delimiter is always a capital letter following a small letter without space. I tried
sed 's/\([a-z][A-Z]\)/\n\1/g <<< String, SomeotherString Additional, StringNew String However this leads to:
String, Someothe
rString Additional, Strin
gNew String
Thanks for your help
>Solution :
With sed:
sed 's/\([a-z]\)\([A-Z]\)/\1\n\2/g'
Matches a small letter (sub-expression 1) followed by a capital letter (sub-expression 2) and replaces them with the part matching sub-expression 1, a newline character, and the part matching sub-expression 2.
If your sed supports it, you can use -E to enable extended regexps, so that you don’t have to put backslashes before the parentheses.
sed -E 's/([a-z])([A-Z])/\1\n\2/g'
At least GNU sed also supports named character classes, so you can easily match other letters than a-z and A-Z too:
sed -E 's/([[:lower:]])([[:upper:]])/\1\n\2/g'