文字列内のn番目の文字の大文字と小文字を変更する

文字列内のn番目の文字の大文字と小文字を変更する

BASHsed文字列(または、、awkなどのような他の* nixツール)trで文字列のn番目の文字の大文字と小文字を変更したいと思います。

以下を使用して文字列全体の大文字と小文字を変更できることを知っています。

${str,,} # to lowercase
${str^^} # to uppercase

"Test" 3番目の文字の大文字と小文字を大文字に変更できますか?

$ export str="Test"
$ echo ${str^^:3}
TeSt

ベストアンサー1

Bashでは、次のことができます。

$ str="abcdefgh"
$ foo=${str:2}  # from the 3rd letter to the end
echo ${str:0:2}${foo^} # take the first three letters from str and capitalize the first letter in foo.
abCdefgh

パールでは:

$ perl -ple 's/(?<=..)(.)/uc($1)/e; ' <<<$str
abCdefgh

または

$ perl -ple 's/(..)(.)/$1.uc($2)/e; ' <<<$str
abCdefgh

おすすめ記事