bash 使用 SED 大写单词的首字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1538676/
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
Uppercasing First Letter of Words Using SED
提问by neversaint
How do you replace the first letter of a word into Capital letter, e.g.
你如何将单词的第一个字母替换为大写字母,例如
Trouble me
Gold rush brides
into
进入
Trouble Me
Gold Rush Brides
回答by tangens
This line should do it:
这一行应该这样做:
sed -e "s/\b\(.\)/\u/g"
回答by ghostdog74
Using awk
:
使用awk
:
awk '{for(i=1;i<=NF;i++){ $i=toupper(substr($i,1,1)) substr($i,2) }}1' file
The output would be:
输出将是:
Trouble Me
Gold Rush Brides
回答by rekha_sri
Use the following sed command for capitalizing the first letter of the each word.
使用以下 sed 命令将每个单词的第一个字母大写。
echo -e "Trouble me \nGold rush brides" | sed -r 's/\<./\U&/g'
output
输出
Trouble Me
Gold Rush Brides
The -r
switch tells sed
to use extended regular expressions. The instructions to sed
then tell it to "search and replace" (the s
at the beginning) the pattern \<.
with the pattern \U&
globally, i.e. all instances in every line (that's the g
modifier at the end). The pattern we're searching for is \<.
which is looking for a word boundary(\<
) followed by any character(.
). The replacement pattern is \U&
, where \U
instructs sed
to make the following text uppercaseand &
is a synonym for \0
, which refers to "everything that was matched". In this case, "everything that was matched" is just what the .
matched, as word boundaries are not included in the matches (instead, they are anchors). What .
matched is just one character, so this is what is upper cased.
该-r
开关通知sed
使用扩展的正则表达式。该指令sed
则告诉“小号地球与替换”(在s
开头)的图案\<.
与图案\U&
摹lobally,即在每一行的所有实例(这是g
在端改性)。我们正在搜索的模式\<.
是寻找单词边界( \<
) 后跟任何字符( .
)。替换模式是\U&
,在\U
指示sed
要进行以下文字大写和&
是一个代名词\0
,它指的是“匹配的所有内容”。在这种情况下,“匹配的所有内容”就是.
匹配的内容,因为单词边界不包含在匹配中(相反,它们是anchors)。什么.
匹配是一个字符,所以这是在上部套管。
回答by mikejonesey
I had apostrophes so, working off the first solution...
我有撇号所以,解决第一个解决方案......
mike@mike-laptop3:~$ echo "BEST WESTERN PLUS BOB's INN" | tr "[A-Z]" "[a-z]" | sed -e "s/\b\(.\)/\u/g"
Best Western Plus Bob'S Inn
贝斯特韦斯特普拉斯鲍勃旅馆
mike@mike-laptop3:~$ echo "BEST WESTERN PLUS BOB's INN" | tr "[A-Z]" "[a-z]" | sed "s/\( \|^\)\(.\)/\u/g"
Best Western Plus Bob's Inn
贝斯特韦斯特鲍勃旅馆
回答by chtimi59
Another shorter version with sed:
另一个带有 sed 的较短版本:
sed -e "s/\b./\u$ echo "HELLO WORLD" | python3 -c "import sys; print(sys.stdin.read().title())"
Hello World
/g"
回答by Havok
I know you said sed, but for shell scripting one of the easiest and more flexible for me was using Python which is available in most systems:
我知道你说的是 sed,但对于 shell 脚本,对我来说最简单、更灵活的方法之一是使用 Python,它在大多数系统中都可用:
$ lorem | python3 -c "import sys; print(sys.stdin.read().title())"
Officia Est Omnis Quia. Nihil Et Voluptatem Dolor Blanditiis Sit Harum. Dolore Minima Suscipit Quaerat. Soluta Autem Explicabo Saepe. Recusandae Molestias Et Et Est Impedit Consequuntur. Voluptatum Architecto Enim Nostrum Ut Corrupti Nobis.
For example:
例如:
$ echo " This iS mY USER ${USER} " | python3 -c "import sys; print(sys.stdin.read().strip().lower().capitalize())"
This is my user jenkins
You can also use things like strip()
to remove spaces, or capitalize()
:
您还可以使用诸如strip()
删除空格之类的方法,或者capitalize()
:
sed 's/\w\+/\L\u&/g'
回答by Skippy le Grand Gourou
Proposed sed
solutions until now will only work if the original text is in lowercase. Although one could use tr '[[:upper:]]' '[[:lower:]]'
to normalize the input to lowercase, it may be convenient to have an all-in-one sed
solution?:
sed
到目前为止,建议的解决方案仅适用于原始文本为小写的情况。虽然可以使用tr '[[:upper:]]' '[[:lower:]]'
将输入规范化为小写,但拥有一个多合一的sed
解决方案可能会很方便?:
msg="this is a message"
for word in $msg
do
echo -n ${word^} ""
done
This Is A Message
This will match words (\w
means word character, and \+
at least one and until the next non-word character), and lowercase until the end (\L
) butuppercase the next (i.e. first) character (\u
) on each (g
) matched expression (&
).
这将匹配单词(\w
表示单词字符,并且\+
至少有一个并且直到下一个非单词字符),并且小写直到结尾 ( \L
)但大写\u
每个 ( g
) 匹配表达式 ( &
)上的下一个(即第一个)字符( )。
[Credits]
[学分]
回答by Dominique Baldo
Using bash (without sed, so a little off topic):
使用 bash(没有 sed,所以有点离题):
name="athens"
echo $name | sed -r "s/^[[:alpha:]]/$(echo ${name:0:1} | tr [[:lower:]] [[:upper:]])/"
回答by mark_infinite
Using sedwith tr:
将sed与tr一起使用:
##代码##