【发布时间】:2020-05-08 21:24:07
【问题描述】:
假设我有一个具有这种结构的目录:
我正在制作一个脚本,在末尾有一个字母“x”的情况下删除字母“x”,以便获得这样的结果:
所以,我正在考虑做一个 For 循环去每个目录,然后取文件夹路径的最后一部分
for d in Fruits_and_Vegetables/*/
do
(cd "$d" && Fruit_or_Vegetable=`basename "$PWD"`)
done
问题是我不知道如何告诉去只拿最后一个目录
然后,我正在考虑修改字符串
echo $Fruit_or_Vegetable | awk '{print substr($1, 1, length($1)-1)}' # substr(s, a, b) : it returns b number of chars from string s, starting at position a.
# The parameter b is optional, in which case it means up to the end of the string.
问题是我不知道如何告诉 AWK 将“Moscato Giallox”视为一个词,因为当我执行命令时,它返回“Moscat Giallox”而不是“Moscato Giallo”......我也猜我必须放置一个if语句来查看最后一个字母是否是x,并且只在这些情况下执行命令。
能给我一些建议吗,谢谢。
【问题讨论】:
-
为什么不使用
rename命令?它允许您使用 Perl 风格的正则表达式匹配和替换。 -
@Barmar,不同的系统有不同的
rename命令。见Why is the rename utility on Debian/Ubuntu different than the one on other distributions, like CentOS?。