Bil*_*oon 6 linux variables shell
我正在写一个shell脚本,并有一个这样的变量:something-that-is-hyphenated.
我需要在脚本中的各个点使用它:
something-that-is-hyphenated,somethingthatishyphenated,SomethingThatIsHyphenated
我已经设法somethingthatishyphenated通过剥离-使用来改变它sed "s/-//g".
我确信有一种更简单的方法,而且还需要知道如何获得驼峰套装版本.
编辑:工作函数源自@Michał的答案
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
Run Code Online (Sandbox Code Playgroud)
编辑:最后,感谢@glenn的sed one liner
echo a-hyphenated-string | sed -E "s/(^|-)([a-z])/\u\2/g"
Run Code Online (Sandbox Code Playgroud)
一个 GNU sed 一行代码
echo something-that-is-hyphenated |
sed -e 's/-\([a-z]\)/\u\1/g' -e 's/^[a-z]/\u&/'
Run Code Online (Sandbox Code Playgroud)
\u替换字符串中的内容记录在sed 手册中。