linux shell标题大小写 [英] linux shell title case
问题描述
我正在编写一个shell脚本,并有一个像这样的变量:something-that-is-hyphenated
.
I am wrinting a shell script and have a variable like this: something-that-is-hyphenated
.
我需要在脚本的各个方面使用它,例如:
I need to use it in various points in the script as:
something-that-is-hyphenated
,somethingthatishyphenated
,SomethingThatIsHyphenated
通过使用sed "s/-//g"
剥离-
,我设法将其更改为somethingthatishyphenated
.
I have managed to change it to somethingthatishyphenated
by stripping out -
using sed "s/-//g"
.
我确信有一种更简单的方法,并且还需要知道如何获得骆驼套式版本.
I am sure there is a simpler way, and also, need to know how to get the camel cased version.
工作函数源自@Michał的答案
Working function derived from @Michał's answer
function hyphenToCamel {
tr '-' '\n' | awk '{printf "%s%s", toupper(substr($0,1,1)), substr($0,2)}'
}
CAMEL=$(echo something-that-is-hyphenated | hyphenToCamel)
echo $CAMEL
最后,感谢@glenn
Finally, a sed one liner thanks to @glenn
echo a-hyphenated-string | sed -E "s/(^|-)([a-z])/\u\2/g"
推荐答案
GNU sed单线版
a GNU sed one-liner
echo something-that-is-hyphenated |
sed -e 's/-\([a-z]\)/\u\1/g' -e 's/^[a-z]/\u&/'
\u
in the replacement string is documented in the sed manual.
这篇关于linux shell标题大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!