匹配和重新格式化输入错误的文件名 [英] matching and reformatting poorly typed filenames
问题描述
我有很多文件名格式不正确,我想清理它们.想法是重新格式化名称,以使它们更合理.
I have a large number of filenames that have been poorly formatted, which I would like to clean up. The idea is to reformat the names so that they are more sensible.
以下是一些示例:
01harpharm_3.1.aiff 1
01harpmute_2.6.aiff 1
01harpmute_2.6.aiff 2
01harpmute_2.6.aiff 3
01harpmute_2.6.aiff 4
如果我们看最后一个例子,目标将是重写
If we look at the last example, the goal would be to rewrite
01harpmute_2.6.aiff 4
作为01harpmute_2.6.4.aiff
.
我想通过脚本(for循环),查找,重命名或其组合来使用bash. 有人会对如何最好地解决这个问题有意见吗?
I would like to use bash for this, either via a script (for loop), find, rename, or a combination thereof. Would anyone have opinions on how to best tackle this?
推荐答案
能否请您尝试一次.以下将仅在屏幕上显示重命名命令,您可以从中运行1条命令,如果对结果满意,则可以运行我的第二条代码.
Could you please try following once. Following will only print the rename commands on screen, you could run 1 command from it and if you are happy with results then you could run my 2nd code.
find . -type f -name "*.aiff*" -print0 |
while IFS= read -r -d '' file
do
echo "$file" |
awk '
BEGIN{ s1="\"" }
{ val=$0; sub(/^\.\//,""); sub(/\.aiff/,"."$2"&",$1)
print "mv " s1 val s1 OFS $1
}'
done
或者,如果要放置条件并检查文件名是否有空格,请在awk
命令中添加一个附加条件,它将跳过名称中没有空格的文件.
OR in case you want to put condition and check if file name has space or not so put an additional condition in awk
command it will skip files which are not having space in their names.
find . -type f -name "*.aiff*" -print0 |
while IFS= read -r -d '' file
do
echo "$file" |
awk '
BEGIN{ s1="\"" }
NF==2{ val=$0; sub(/^\.\//,""); sub(/\.aiff/,"."$2"&",$1)
print "mv " s1 val s1 OFS $1
}'
done
请仅在成功测试以上代码后再运行以下程序.
Run following only after you have tested above code successfully please.
find . -type f -name "*.aiff*" -print0 |
while IFS= read -r -d '' file
do
echo "$file" |
awk '
BEGIN{ s1="\"" }
{ val=$0; sub(/^\.\//,""); sub(/\.aiff/,"."$2"&",$1)
print "mv " s1 val s1 OFS $1
}' | sh
done
这篇关于匹配和重新格式化输入错误的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!