gpt4 book ai didi

linux - 如果在一行中找到大写字母则拆分行

转载 作者:太空宇宙 更新时间:2023-11-04 10:35:27 24 4
gpt4 key购买 nike

我需要一个正则表达式,如果找到大写字母,它可以拆分行。

例子:-

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

期望输出如下

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

最佳答案

是你想要的吗?

$ 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

请注意,在 JOHN 之前添加了一个换行符,因为它符合您的要求。避免它是另一个问题。另外你的要求是:

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

所以预期的输出应该是:

$ 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

关于linux - 如果在一行中找到大写字母则拆分行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37831227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com