Home > Software design >  How to split Lines using shell SED or something similar
How to split Lines using shell SED or something similar

Time:07-15

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

CodePudding user response:

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.

The previous should work with any sed. With GNU sed and others that support it, you can use -E (also -r in GNU sed) 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'

CodePudding user response:

More than one way to do this, but here's one that uses perl

echo 'StringSomeotherstringAdditionalString' | perl -pe 's/([A-Z])/\n$1/g'

[A-Z] matches a capital letter; \n$1 replaces it with a newline and the capital letter.

CodePudding user response:

Using sed

$ sed 's/, [A-Z][^A-Z]*/&\n/g' input_file
String, Someother
String Additional, String
New String
  • Related