gpt4 book ai didi

shell - 在 bash shell 脚本中将字符串转换为字符串的小写部分

转载 作者:行者123 更新时间:2023-12-01 09:32:17 25 4
gpt4 key购买 nike

我有一个 file.txt 文件的每一行都像:

ABLED   EY B AH L D
ABLER EY B AH L ER

我想要每行的第二部分: EY B AH L DEY B AH L ER ,例如,在小写,保持其余大写。我该怎么做?

非常感谢您提前。

最佳答案

while read first second; do
second=$(echo "$second" | tr [:upper:] [:lower:])
printf '%s\t%s\n' "$first" "$second"
done < file.txt

输出:
ABLED   ey b ah l d
ABLER ey b ah l er

在 KornShell、pdksh 或 Bash 中执行此操作的另外两种方法,无需调用 tr
在变量上设置“小写”标志(仅限 KornShell 和兼容的 shell):
typeset -l second
while read first second; do
printf '%s\t%s\n' "$first" "$second"
done < file.txt

使用 Bash 的 case-modification 参数扩展修饰符(仅限 Bash!):
while read first second; do
printf '%s\t%s\n' "$first" "${second,,}"
done < file.txt

关于shell - 在 bash shell 脚本中将字符串转换为字符串的小写部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13935395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com