简体   繁体   English

如何使用 shell SED 或类似的东西分割线

[英]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:我想要以下 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/\([az][AZ]\)/\n\1/g <<< String, SomeotherString Additional, StringNew String However this leads to:我尝试sed 's/\([az][AZ]\)/\n\1/g <<< String, SomeotherString Additional, StringNew String但是这会导致:

String, Someothe
rString Additional, Strin
gNew String

Thanks for your help谢谢你的帮助

More than one way to do this, but here's one that uses perl不止一种方法可以做到这一点,但这里有一种使用 perl

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

[AZ] matches a capital letter; [AZ]匹配大写字母; \n$1 replaces it with a newline and the capital letter. \n$1用换行符和大写字母替换它。

With sed:使用 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.匹配小写字母(子表达式 1)后跟大写字母(子表达式 2),并将它们替换为匹配子表达式 1 的部分、换行符和匹配子表达式 2 的部分。

The previous should work with any sed.前一个应该适用于任何 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.使用 GNU sed 和其他支持它的工具,您可以使用-E (在 GNU sed 中也是-r )来启用扩展的正则表达式,这样您就不必在括号前放置反斜杠。

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 az and AZ too:至少 GNU sed 还支持命名字符类,因此您也可以轻松匹配 az 和 AZ 以外的其他字母:

sed -E 's/([[:lower:]])([[:upper:]])/\1\n\2/g'

Using sed使用sed

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM