Wac*_*Get 2 script bash shell-script
我有一个文件文件夹,我想在第 5 个位置更改其中一个字符的大小写。
由此:
ABC-xyz
DEF-xyz
GHI-xys
Run Code Online (Sandbox Code Playgroud)
对此:
ABC-Xyz
DEF-Xyz
GHI-Xys
Run Code Online (Sandbox Code Playgroud)
您会注意到 X 已转换为大写。
任何想法我如何在 Bash 中做到这一点?
纯 bash 示例:
#!/usr/bin/env bash
for f in *; do
g="${f::4}" ##Split the first four characters
h="${f:4:1}" ##just the fifth character (starts counting at 0)
i="${f:5}" ## character 6+ (again, counting from 0)
mv -- "$f" "$g${h^^}$i"
##At the end, put the strings back together
##but make $h (character 5) uppercase
done
exit 0
Run Code Online (Sandbox Code Playgroud)
实际上,我可能会使用 perl-rename (rename在 Ubuntu 存储库中调用;我知道在其他一些中它会通过prename):
rename 's/(.{4})(.)/$1\u$2/' *
Run Code Online (Sandbox Code Playgroud)