bash 使用正则表达式数组替换重命名

发布于 2024-10-09 12:49:45 字数 534 浏览 0 评论 0原文

我有一个与 这篇文章

我想知道如何使用指定的替换来重命名文件名中出现的事件。例如,如果原始文件名为:“the Quick Brown Quick Brown Fox.avi”,我想将其重命名为“The Slow Red Slow Red Fox.avi”。

我尝试过这个:

new="(quick=>'slow',brown=>'red')"
regex="quick|brown"
rename -v "s/($regex)/$new{$1}/g" *

但没有爱:(

我也尝试过,

regex="qr/quick|brown/"

但这只会给出错误。知道我做错了什么吗?

i have a very similar question as for this post.

i would like to know how to rename occurances within a filename with designated substitutions. for example if the original file is called: 'the quick brown quick brown fox.avi' i would like to rename it to 'the slow red slow red fox.avi'.

i tried this:

new="(quick=>'slow',brown=>'red')"
regex="quick|brown"
rename -v "s/($regex)/$new{$1}/g" *

but no love :(

i also tried with

regex="qr/quick|brown/"

but this just gives errors. any idea what im doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2024-10-16 12:49:45

根据您的示例,我认为您需要多个替换(不仅仅是将“快速棕色”转换为“缓慢红色”,而是将单词列表转换为新单词列表。您可以用分号分隔替换。这是一个解决方案适用于您的示例:

rename -v 's/quick/slow/g;s/brown/red/g' *

如果您真的想使用数组将旧字符串映射到新字符串,您可以将更多 Perl 塞入参数中以重命名(但在某些时候您可能只是将 Perl 脚本编写为独立脚本):

rename -v '%::new=(quick=>"slow",brown=>"red");s/(quick|brown)/$::new{$1}/g' *

Based on your example, I think you want multiple substitutions (not just converting "quick brown" to "slow red" but converting a list of words to a list of new words. You can separate the substitutions with a semicolon. Here's a solution that works for your example:

rename -v 's/quick/slow/g;s/brown/red/g' *

And if you're really bent on using an array to map the old strings to the new string, you can cram even more Perl into the argument to rename (but at some point you might just write the Perl script as a stand-alone script):

rename -v '%::new=(quick=>"slow",brown=>"red");s/(quick|brown)/$::new{$1}/g' *
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文