[英]Capitalize words in a bash variable using sed
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. 我正在寻找的是如何使用
sed
命令修改变量内的文本。 To be honest, I don't really know if that possible with sed
. 老实说,我真的不知道
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
进行修改,是否有其他方法可以达到相同的结果?
string=$(sed 's/^./\u&/; s/-\(.\)/ \u\1/g' <<< $string)
Example: 例:
sdlcb@Goofy-Gen:~/AMD$ cat File
#!/bin/bash
string="this-is-my-string"
string=$(sed 's/^./\u&/; s/-\(.\)/ \u\1/g' <<< $string)
echo "$string"
AMD$ ./File
This Is My String
s/^./\\u\u0026amp;/
=> Capitalize the first character \\u\u003c/code> for uppercase.
s/^./\\u\u0026amp;/
=>将第一个字符\\u\u003c/code>大写。
&
=> the matched pattern. &
=>匹配的模式。
s/-\\(.\\)/ \\u\\1/g
=> substitute - followed by character
to space followed by uppercase of the character
. s/-\\(.\\)/ \\u\\1/g
=>替换- followed by character
到space followed by uppercase of the character
。 ( )
used to group pattern and \\1
=> first group, in this case only 1 such group is present. ( )
用于对模式和\\1
=>第一组进行分组,在这种情况下,仅存在1个这样的组。
Yeah that's possible, try something like: 是的,可以尝试以下方法:
#!/bin/bash
string="this-is-my-string"
string=$(echo ${string} | sed 's/\-/ /g')
echo ${string}
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
If you are interested this is quite simple in Python
, from the interpreter
: 如果您对此感兴趣,可以使用
interpreter
在Python
非常简单:
>>> string = "this-is-my-string"
>>> string = ' '.join([ s.capitalize() for s in string.split('-')])
>>> print string
This Is My String
You can try it like this too 您也可以这样尝试
echo '"this-is-my-string"'|sed 's/-/ /g;s/\([^"]\)\(\S*\s*\)/\u\1\2/g'
or 要么
echo "$string"|sed 's/-/ /g;s/\([^"]\)\(\S*\s*\)/\u\1\2/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 \\S
是一个元字符,它匹配不是空格/空白\\s
任何字符, \\s
则是一个空格的任何字符
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.