脊髓案例,以案例的骆驼 [英] Spinal Case to Camel Case
问题描述
我的问题是pretty的这么多相反:<一href=\"http://stackoverflow.com/questions/8502977/linux-bash-camel-case-string-to-separate-by-dash\">linux庆典,骆驼字符串由破折号分开
My question is pretty much the opposite of this: linux bash, camel case string to separate by dash
pretty热切采取部分串样这样
到 SomeStringLikeThis
。
Pretty much looking to take some-string-like-this
to SomeStringLikeThis
.
任何人都得到了一些 SED
魔法还是很容易这样做的其他方式?
Anyone got some sed
magic or other means of doing this easily?
作为一个方面说明,我的一部分认为,作为流行Bash是,这有可能是一个图书馆,在那里,可以像这样转换的帮助...我还没有找到一个,但。如果你知道的人,请让我知道。例如这将处理标准的命名风格,如脊柱强调之间的共同字符串操作/转换出库,强调以骆驼,骆驼脊柱等。
As a side note, part of me thinks that as popular as Bash is, that there might be a library out there that could help with conversions like this... I haven't found one though. If you know of one, please let me know. e.g. a library that would handle common string manipulations/conversion between standard naming styles, such as spinal to underscore, underscore to camel, camel to spinal, etc.
推荐答案
这适用于GNU sed的:
This works with GNU sed:
sed -r 's/(^|-)(\w)/\U\2/g'
匹配线或的开始 -
后面跟着一个字母数字字符,并使用 \\ U
来使字符大写。
Match the start of the line or a -
followed by an alphanumeric character and use \U
to make the character uppercase.
这是几乎相同的在Perl:
It's almost identical in perl:
perl -pe 's/(^|-)(\w)/\U$2/g'
只是为了好玩,这里有一个方法,你可以做它在本地的bash:
Just for fun, here's a way you could do it in native bash:
spinal_to_upper() {
IFS=- read -ra str <<<"$1"
for s in "${str[@]}"; do
printf '%s' "${s^}"
done
}
spinal_to_upper "some-string-like-this"
这篇关于脊髓案例,以案例的骆驼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!