Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split Lines using shell SED or something similar

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading