使用 sed 将 bash 变量中的单词大写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27703739/
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
Capitalize words in a bash variable using sed
提问by Merianos Nikos
I am totally new on shell scripting (actually three days only :) ) so don't shoot me .. !!
我对shell脚本完全陌生(实际上只有三天:))所以不要拍我..!!
In my script I have a code that looks like that:
在我的脚本中,我有一个如下所示的代码:
string="this-is-my-string"
and I like to modify the content of the variable, in order to become like that:
我喜欢修改变量的内容,以便变成这样:
string="This Is My String"
I don't look for the regex that modify the original string into the modified version. I already have find the solution here.
我不寻找将原始字符串修改为修改版本的正则表达式。我已经在这里找到了解决方案。
What I am looking for, is how to modify the text inside the variable using the sed
command. To be honest, I don't really know if that possible with sed
. In case that modification it is not possible to be done with sed
is there any other method to achieve the same result ?
我正在寻找的是如何使用sed
命令修改变量内的文本。说实话,我真的不知道这是否可能与sed
. 如果无法进行修改,sed
是否还有其他方法可以达到相同的结果?
回答by Arjun Mathew Dan
string=$(sed 's/^./\u&/; s/-\(.\)/ \u/g' <<< $string)
Example:
例子:
sdlcb@Goofy-Gen:~/AMD$ cat File
#!/bin/bash
string="this-is-my-string"
string=$(sed 's/^./\u&/; s/-\(.\)/ \u/g' <<< $string)
echo "$string"
AMD$ ./File
This Is My String
s/^./\u&/
=> Capitalize the first character \u
for uppercase. &
=> the matched pattern.
s/^./\u&/
=> 大写第一个字符\u
。&
=> 匹配的模式。
s/-\(.\)/ \u\1/g
=> substitute - followed by character
to space followed by uppercase of the character
. ( )
used to group pattern and \1
=> first group, in this case only 1 such group is present.
s/-\(.\)/ \u\1/g
=> 替换- followed by character
为space followed by uppercase of the character
. ( )
用于对模式和\1
=> 第一组进行分组,在这种情况下,只有 1 个这样的组存在。
回答by gniourf_gniourf
A pure Bash (Bash≥4) solution with parameter expansions:
带有参数扩展的纯 Bash (Bash≥4) 解决方案:
$ string='this-is-my-string'
$ IFS=- read -r -a ary <<< "$string"
$ string="${ary[@]^}"
$ echo "$string"
This Is My String
回答by Jasper
Yeah that's possible, try something like:
是的,这是可能的,请尝试以下操作:
#!/bin/bash
string="this-is-my-string"
string=$(echo ${string} | sed 's/\-/ /g')
echo ${string}
回答by Juan Diego Godoy Robles
If you are interested this is quite simple in Python
, from the interpreter
:
如果您有兴趣,这很简单Python
,来自interpreter
:
>>> string = "this-is-my-string"
>>> string = ' '.join([ s.capitalize() for s in string.split('-')])
>>> print string
This Is My String
回答by repzero
You can try it like this too
你也可以这样试试
echo '"this-is-my-string"'|sed 's/-/ /g;s/\([^"]\)\(\S*\s*\)/\u/g'
or
或者
echo "$string"|sed 's/-/ /g;s/\([^"]\)\(\S*\s*\)/\u/g'
the \S
is a metacharacter that matches any character that is not a space/whitespace
and \s
matches any characters that is a space
the\S
是一个元字符,它匹配任何非空格/空格的字符并\s
匹配任何空格字符
回答by Jotne
Here is an awk
solution:
这是一个 awk
解决方案:
echo "this-is-my-string" | awk -F- '{for (i=1;i<=NF;++i) $i=toupper(substr($i,1,1)) substr($i,2)}1'
This Is My String
string='this-is-my-string'
string=$(awk -F- '{for (i=1;i<=NF;++i) $i=toupper(substr($i,1,1)) substr($i,2)}1' <<< "$string")
echo "$string"
This Is My String
Here is another awk
这是另一个 awk
echo "this-is-my-string" | awk -F- '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1'
This Is My String