gpt4 book ai didi

bash 删除文件名的一部分

转载 作者:行者123 更新时间:2023-11-29 08:52:18 25 4
gpt4 key购买 nike

我有以下格式的文件:

$ ls CombinedReports_LLL-*'('*.csv
CombinedReports_LLL-20140211144020(Untitled_1).csv
CombinedReports_LLL-20140211144020(Untitled_11).csv
CombinedReports_LLL-20140211144020(Untitled_110).csv
CombinedReports_LLL-20140211144020(Untitled_111).csv
CombinedReports_LLL-20140211144020(Untitled_12).csv
CombinedReports_LLL-20140211144020(Untitled_13).csv
CombinedReports_LLL-20140211144020(Untitled_14).csv
CombinedReports_LLL-20140211144020(Untitled_15).csv
CombinedReports_LLL-20140211144020(Untitled_16).csv
CombinedReports_LLL-20140211144020(Untitled_17).csv
CombinedReports_LLL-20140211144020(Untitled_18).csv
CombinedReports_LLL-20140211144020(Untitled_19).csv

我想删除这部分:
20140211144020(这是运行报告的时间戳,因此会有所不同)

最后是这样的:

CombinedReports_LLL-(Untitled_1).csv
CombinedReports_LLL-(Untitled_11).csv
CombinedReports_LLL-(Untitled_110).csv
CombinedReports_LLL-(Untitled_111).csv
CombinedReports_LLL-(Untitled_12).csv
CombinedReports_LLL-(Untitled_13).csv
CombinedReports_LLL-(Untitled_14).csv
CombinedReports_LLL-(Untitled_15).csv
CombinedReports_LLL-(Untitled_16).csv
CombinedReports_LLL-(Untitled_17).csv
CombinedReports_LLL-(Untitled_18).csv
CombinedReports_LLL-(Untitled_19).csv

我只是在想 mv命令,可能是这样的:

$ ls CombinedReports_LLL-*'('*.csv

但也许是 sed命令或其他会更好

最佳答案

renameperl 包的一部分。它根据 perl 风格的正则表达式重命名文件。要从文件名中删除日期:

rename 's/[0-9]{14}//' CombinedReports_LLL-*.csv

如果rename不可用,可以使用sed+shell:

for fname in Combined*.csv ; do mv "$fname" "$(echo "$fname" | sed -r 's/[0-9]{14}//')" ; done

上面的循环遍历了你的每个文件。对于每个文件,它执行一个 mv 命令:mv "$fname""$(echo "$fname"| sed -r 's/[0-9]{14}//')" 其中,在这种情况下,sed 能够使用与上面的 rename 命令相同的正则表达式。 s/[0-9]{14}// 告诉 sed 查找连续的 14 位数字并将它们替换为空字符串。

关于bash 删除文件名的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21691864/

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