正则表达式在 alpine linux 中制作单词驼峰式大小写
Regex to make a word camel case in alpine linux
我已经在 centos 中尝试过这个正则表达式
正则表达式:echo 'select-value'|sed -r 's/(-)(\w)/\U/g'
输出:selectValue
但是在 alpine 中,当我尝试下面的正则表达式时我没有得到输出
正则表达式:echo 'select-value'|sed -r 's/(-)(\w)/\%U/g'
输出:select%Uvalue
预期输出:selectValue
请建议正确的正则表达式。
谢谢
如果您不会使用sed,试试perl:
$ echo 'select-value-but-not-that-one'|perl -pe 's/-(\w)/\u/g'
selectValueButNotThatOne
看来alpine默认使用busybox sed。您必须安装 gnu sed(它可能已经安装)。
在 docker alpine 3.5 容器中,我已经尝试过那个并且它工作 echo 'select-value'|/bin/sed -r 's/(-)(\w)/\U/g'
。注意 /bin/sed
部分。 Gnu sed 已安装在 docker 映像中。
如果你运气不好,你可以使用 awk
(如果可用):
echo 'select-value-another-value'|awk -F'-' '{ for(i=1; i<=NF; i++) printf toupper(substr($i,1,1)) substr($i,2);printf "\n"}'
我已经在 centos 中尝试过这个正则表达式
正则表达式:echo 'select-value'|sed -r 's/(-)(\w)/\U/g'
输出:selectValue
但是在 alpine 中,当我尝试下面的正则表达式时我没有得到输出
正则表达式:echo 'select-value'|sed -r 's/(-)(\w)/\%U/g'
输出:select%Uvalue
预期输出:selectValue
请建议正确的正则表达式。
谢谢
如果您不会使用sed,试试perl:
$ echo 'select-value-but-not-that-one'|perl -pe 's/-(\w)/\u/g'
selectValueButNotThatOne
看来alpine默认使用busybox sed。您必须安装 gnu sed(它可能已经安装)。
在 docker alpine 3.5 容器中,我已经尝试过那个并且它工作 echo 'select-value'|/bin/sed -r 's/(-)(\w)/\U/g'
。注意 /bin/sed
部分。 Gnu sed 已安装在 docker 映像中。
如果你运气不好,你可以使用 awk
(如果可用):
echo 'select-value-another-value'|awk -F'-' '{ for(i=1; i<=NF; i++) printf toupper(substr($i,1,1)) substr($i,2);printf "\n"}'