John John - 5 months ago 10
Linux Question

Split Lines If Uppercase letter Found in a line

I need a regex which could split the lines if Uppercase letter found.

Example :-

line1 = JOHN levin have fun RAJESH is a good person SAM was ok


Exapecting the output as below

line1 = JOHN levin have fun
RAJESH is a good person
SAM was ok.

Answer

Is what you want?

$ line1='JOHN levin have fun RAJESH is a good person SAM was ok'
$ sed 's/[A-Z]\+/\n&/g' <<< $line1

JOHN levin have fun
RAJESH is a good person
SAM was ok

Notice that a newline is added before JOHN since it matches your requirement. Avoid it is another question. Also your requirement is:

I need a regex which could split the lines if Uppercase letter found.

So the expected output should be:

$ sed 's/\([A-Z]\)/\n\1/g' <<< $line1

J
O
H
N levin have fun
R
A
J
E
S
H is a good person
S
A
M was ok
Comments