英文:
linux command to transform 'VW_ABCD_EF_GHIJ_KLM_...' into 'AbcdEfGhjKlm...'
问题
I am trying to modify a string with the pattern 'VW_ABCD_EF_GHIJ_KLM_...' into 'AbcdEfGhjKlm...'.
For example "s=VW_ABCD_EF_GHIJ_KLM" must become "AbcdEfGhjKlm".
So far I got s=VW_ABCD_EF_GHIJ_KLM; echo "${s,,}" | sed -e 's/vw\(.*\)/\1/g'
which produces "_abcd_ef_ghij_klm".
I think I would have to create a variant number of groups for (.*) and replace '[a-z]' for '[A-Z]' in every group, but I could not find how to do it.
英文:
I am trying to modify a string with the pattern 'VW_ABCD_EF_GHIJ_KLM_...' into 'AbcdEfGhjKlm...'
For example "s=VW_ABCD_EF_GHIJ_KLM" must become "AbcdEfGhjKlm".
So far I got s=VW_ABCD_EF_GHIJ_KLM; echo "${s,,}" | sed -e 's/vw\(.*\)/\1/g'
which produces "_abcd_ef_ghij_klm".
I think I would have to create a variant number of groups for \(_.*\)
and replace '_[a-z]' for '[A-Z]' in every group, but I could not find how to do it
答案1
得分: 2
对 OP 当前的代码进行扩展,以去除 _
并将 _
后面的字母大写:
$ s='VW_ABCD_EF_GHIJ_KLM'
$ sed -e 's/vw\(.*\)//g;s/_\(.\)/\U/g' <<< "${s,,}"
AbcdEfGhijKlm
在限制 VW
移除到字符串开头的情况下,可实现同等效果:
$ s='VW_ABCD_EF_GHIJ_KLM'
$ sed -E 's/^vw//g;s/_(.)/\U/g' <<< "${s,,}"
AbcdEfGhijKlm
注意: 假设每个 _
后面跟着一个字母(如 OP 示例)。
英文:
Expanding on OP's current code to strip the _
and U
ppercase the letter following the _
:
$ s='VW_ABCD_EF_GHIJ_KLM'
$ sed -e 's/vw\(.*\)//g;s/_\(.\)/\U/g' <<< "${s,,}"
AbcdEfGhijKlm
One equivalent while limiting the VW
removal to the start of the string:
$ s='VW_ABCD_EF_GHIJ_KLM'
$ sed -E 's/^vw//g;s/_(.)/\U/g' <<< "${s,,}"
AbcdEfGhijKlm
NOTE: assumes each _
is followed by a letter (as in OP's example)
答案2
得分: 2
A slightly different solution, without the initial ${s,,}
processing:
$ s='VW_ABCD_EF_GHIJ_KLM'
$ echo "$s" | sed -E 's/^VW//; s/_([^_]+)/\L\u/g'
AbcdEfGhijKlm
\L\u\1
将使首字母大写,其余小写。
但如果输入不总是大写的:
$ s='Vw_ABcd_ef_GHIJ_KLM'
$ echo "$s" | sed -E 's/^vw(.*)/\L/I; s/_(.)/\u/g'
AbcdEfGhijKlm
英文:
A slightly different solution, without the initial ${s,,}
processing:
$ s='VW_ABCD_EF_GHIJ_KLM'
$ echo "$s" | sed -E 's/^VW//; s/_([^_]+)/\L\u/g'
AbcdEfGhijKlm
\L\u\1
will uppercase the first character and remaining are lowercased.
But if the input isn't always uppercase:
$ s='Vw_ABcd_ef_GHIJ_KLM'
$ echo "$s" | sed -E 's/^vw(.*)/\L/I; s/_(.)/\u/g'
AbcdEfGhijKlm
答案3
得分: 1
以下是翻译好的部分:
这里有一个使用 perl
的版本,而不是依赖于 GNU sed
和非标准的 \u
/等转义字符:
$ s=VW_ABCD_EF_GHIJ_KLM
$ perl -ne 'my @a = map ucfirst, split(/_/, lc); print @a[1..$#a]' <<<"$s"
AbcdEfGhijKlm
当然,perl
也支持 GNU sed
风格的正则表达式大小写转换:
$ perl -pe 's/^VW//; s/_([^_])([^_]*)/\u\L/g' <<<"$s"
AbcdEfGhijKlm
这两个变体,与 GNU sed
的方式不同,对字母进行了标题大小写转换,而不仅仅是简单的大写。对于ASCII字符来说是相同的,但如果你需要扩展到其他字符集,这可能会有所不同。
英文:
Here's a version that uses perl
instead of depending on GNU sed
and it's non-standard \u
/etc. escapes:
$ s=VW_ABCD_EF_GHIJ_KLM
$ perl -ne 'my @a = map ucfirst, split(/_/, lc); print @a[1..$#a]' <<<"$s"
AbcdEfGhijKlm
Of course, perl
also supports GNU sed
-style case-transformation in a regular expression:
$ perl -pe 's/^VW//; s/_([^_])([^_]*)/\u\L/g' <<<"$s"
AbcdEfGhijKlm
Both of these variants, unlike the GNU sed
ones, do title casing of the letters, not simple upper casing. It's the same thing when it comes to ASCII characters, but if you ever have to expand to alphabets beyond that, it can make a difference.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论