从文件名中提取数字,忽略扩展名
我正在制作一个脚本来重命名一些视频文件。有些名为 XXX blah blah.ext
,有些名为 XXX - XXX blah blah.ext
,其中“X”是数字。此外,有些文件是 .avi,有些文件是 mp4。我想要的是从这些文件中提取数字,如果有多个文件,则用空格分隔,并忽略“.mp4”文件中的“4”。
我当前的实现是egrep -o "[[:digit:]]*",虽然这确实将数字分隔成不同的输出,但它也考虑了“.mp4”。
使用 sed 我不仅无法为每个数字生成不同的输出,而且还包括“4”。注意:我对 sed 很陌生,即我开始学习它是为了编写这个脚本。
我该怎么做?
I'm making a script to rename some video files. Some are named XXX blah blah.ext
and some are XXX - XXX blah blah.ext
where "X" are digits. Furthermore, some files are .avi and some are mp4. What I'd like is to extract the numbers from these files, separated by a space if there is more than one, and to disregard the "4" in ".mp4" files.
My current implementation is egrep -o "[[:digit:]]*"
, and while this does separate numbers into different outputs, it also considers ".mp4".
Using sed I've not only not been able to produce different outputs for every number, but it also includes the "4". Note: I'm very new to sed i.e. I began learning it for the purpose of writing this script.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该发现这非常强大:
如果您的
sed
支持-r
,您可以消除用于转义的反斜杠:演示:
这取决于是否存在空格在文件名中第二个数字之前(如果有的话)。如果有文件没有这个,那么简单的修改就可以使其工作。
You should find this to be pretty robust:
If your
sed
supports-r
, you can eliminate the backslashes which are used for escaping:Demo:
This depends on there being a space in the filename before the second number (when there is one). If there are files that don't have that, then a simple modification can make it work.
这可能对你有用:
This might work for you: