BASH - 使第一个字母大写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3872323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BASH - Make the first Letter Uppercase
提问by fwaechter
I try to capitalize the first letter in a CSV which is sorted like this:
我尝试将 CSV 中的第一个字母大写,其排序方式如下:
a23;asd23;sdg3
a23;asd23;sdg3
What i want is a output like this
我想要的是这样的输出
a23;Asd23;Sdg3
a23;Asd23;SDG3
So the first String should be as is, but the second and third should have a capitalized first letter. I tried with AWK and SED but i didn't find the right solution. Can someone help?
所以第一个 String 应该保持原样,但第二个和第三个应该有一个大写的第一个字母。我尝试了 AWK 和 SED,但没有找到正确的解决方案。有人可以帮忙吗?
回答by Bart Sas
Just capitilise all letters that follow a semicolon:
只需将分号后面的所有字母大写:
sed -e 's/;./\U&\E/g'
回答by enzotib
Bash (version 4 and up) has a "first uppercase" operator, ${var^}, but in this case I think it is better to use sed:
Bash(版本 4 及更高版本)有一个“第一个大写”运算符${var^},但在这种情况下,我认为最好使用sed:
sed -r 's/(^|;)(.)/\U/g' <<< "a23;asd23;sdg3"
回答by ghostdog74
$ var="a23;asd23;sdg3"
$ echo $var | awk -F";" '{for(i=2;i<=NF;i++) $i=toupper(substr($i,i,1))substr($i,1) }1' OFS=";"
a23;Sasd23;Gsdg3
回答by eumiro
echo "a23;asd23;sdg3" | perl -ne 's/(?<=\W)(\w)/ uc() /gex;print $_'
a23;Asd23;Sdg3

