将sed或awk中的字符串大写 [英] Capitalize strings in sed or awk
问题描述
我想在bash脚本中使用三种大写的字符串.我认为sed/awk是我最好的选择,但我不确定.鉴于以下要求,最好的方法是什么?
I have three types of strings that I'd like to capitalize in a bash script. I figured sed/awk would be my best bet, but I'm not sure. What's the best way given the following requirements?
-
单字
例如taco -> Taco
single word
e.g.taco -> Taco
用连字符分隔的多个单词
例如my-fish-tacos -> My-Fish-Tacos
multiple words separated by hyphens
e.g. my-fish-tacos -> My-Fish-Tacos
用下划线分隔的多个单词
例如my_fish_tacos -> My_Fish_Tacos
multiple words separated by underscores
e.g. my_fish_tacos -> My_Fish_Tacos
推荐答案
不需要使用捕获组(尽管&
是一个捕获组):
There's no need to use capture groups (although &
is a one in a way):
echo "taco my-fish-tacos my_fish_tacos" | sed 's/[^ _-]*/\u&/g'
输出:
Taco My-Fish-Tacos My_Fish_Tacos
转义的小写字母"u"将匹配的子字符串中的下一个字符大写.
The escaped lower case "u" capitalizes the next character in the matched sub-string.
这篇关于将sed或awk中的字符串大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!