sed 和重命名文件 [英] Sed and renaming files

查看:68
本文介绍了sed 和重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个目录中有大量文件,每个文件都有不同的名称和不同的长度

I have a bulk of files in a directory, each one with different name and different length

那种医师... Class 218 (1080p_30fps_H264-128kbit_AAC).mp4
另一种类型... Class 223 (1080p_30fps_H264-128kbit_AAC).mp4

Kind of Physician ... Class 218 (1080p_30fps_H264-128kbit_AAC).mp4
Another type of ... Class 223 (1080p_30fps_H264-128kbit_AAC).mp4
etc

我只想对Class 218"Class 1"等进行排序并重命名这些文件你们能帮我使用 sed 吗?

I want to sort just "Class 218" "Class 1" etc and rename those files Could you guys help me using sed ?

推荐答案

我会使用 Perl rename 脚本.因此,在这种特定情况下,我将捕获 "Class" 后跟一个或多个空格和一个或多个数字作为我的捕获组,然后使用它重命名文件,同时保留相同的扩展名:

I would use the Perl rename script. So, in this specific case, I would capture "Class" followed by one or more spaces and one or more digits as my capture group and then rename files with that whilst retaining the same extension:

rename --dry-run 's/.*(Class\s+\d+).*/$1.mp4/' *mp4

样本输出

'Another type of ... Class 223 (1080p_30fps_H264-128kbit_AAC).mp4' would be renamed to 'Class 223.mp4'
'Kind of Physician something Class 218 (1080p_30fps_H264-128kbit_AAC).mp4' would be renamed to 'Class 218.mp4'

如果看起来不错,请删除 --dry-run 并再次真正运行.

If that looks good, remove the --dry-run and run again for real.

注意在正则表达式中:

  • .* 表示任意重复任意次数的任意字符或数字,

  • .* means any character or digit repeated any number of times,

/s+ 表示任何类型的空格(制表符或空格)至少出现一次,也可能多次出现,

/s+ means any type of space (tab or space) occurring at least once and possibly more than once,

/d+ 表示任何数字 (0-9) 至少出现一次,也可能出现多次,

/d+ means any digit (0-9) occurring at least once and possibly more than once,

(...) 表示捕获括号内的所有内容,

(...) means capture everything inside the parentheses,

$1 表示替换为之前在括号中捕获的第一项.

$1 means replace with the first thing previously captured in parentheses.

这篇关于sed 和重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆