Perl 的重命名 - 如何附加计数器?

发布于 2024-12-17 14:35:59 字数 809 浏览 1 评论 0原文

Perl 安装时附带了一个不错的重命名实用程序。如何在 Perl 正则表达式中附加一个计数器?这是一个相关问题,例如当前目录中文件编号的问题:

rename 's/^/<here I'd like to have a number of a file being processed>/g' * 

例如,如何将:重命名

fileA
fileB
fileC

1 - fileA
2 - fileB
3 - fileC

编辑

我添加了计数器功能($c 变量) - 请参阅此处。它工作正常 - 但是当我尝试指定计数器格式时:

rename_c.pl -c 5.2f 's/^/$c - /' * 

它说:

 Useless use of concatenation (.) or string in void context at line 120. 

并且它并没有真正使用我告诉它使用的格式。这一定是第 120 行中的一些简单语法错误。您能看一下吗?

There's a nice renaming utility, which comes with Perl's installation. How one would append a counter in the Perl regexp? This is a relevant question e.g. for a problem of numbering files in the current directory:

rename 's/^/<here I'd like to have a number of a file being processed>/g' * 

For example how would one rename:

fileA
fileB
fileC

to

1 - fileA
2 - fileB
3 - fileC

Edit:

I've added the counter feature ($c variable) - see here. It works fine - but when I try to specify the counter format:

rename_c.pl -c 5.2f 's/^/$c - /' * 

it says:

 Useless use of concatenation (.) or string in void context at line 120. 

and it doesn't really use the format I told it to use. This must be some simple syntax mistake in a line number 120. Can You please take a look?

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

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

发布评论

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

评论(4

小嗲 2024-12-24 14:35:59

基本的重命名实用程序可以很好地处理这种情况:

$ rename '$_ = sprintf "%d - %s", ++$count, $_' files...

The basic rename utility can handle that situation just fine:

$ rename '$_ = sprintf "%d - %s", ++$count, $_' files...
千寻… 2024-12-24 14:35:59

有问题的代码行是:

$c = sprintf(eval("%" . "$form", $cNumber));

您不希望有 eval ;您可以简单地将格式创建为字符串:

$c = sprintf("%$form", $cNumber));

字符串(第一个参数)最终包含所请求的格式,并且 sprintf() 使用该格式格式化 $cNumber

The line of code in question is:

$c = sprintf(eval("%" . "$form", $cNumber));

You don't want the eval there; you can simply create the format as a string:

$c = sprintf("%$form", $cNumber));

The string (first argument) ends up containing the format requested, and sprintf() formats $cNumber using that.

宣告ˉ结束 2024-12-24 14:35:59

如果您在 macOS 上并使用通过 brew install rename 安装的工具,则以下内容应该有效:

rename -N ...01 -X -e '$_ = "${_}-counter-$N"' your_files

示例:

> rename -n -N ...01 -X -e '$_ = "${_}-counter-$N"' *.png
'iPad-Pro12.9-4g at 11.24.52.png' would be renamed to 'iPad-Pro12.9-4g at 11.24.52-counter-01.png'
'iPad-Pro12.9-4g at 11.25.09.png' would be renamed to 'iPad-Pro12.9-4g at 11.25.09-counter-02.png'
'iPad-Pro12.9-4g at 11.25.17.png' would be renamed to 'iPad-Pro12.9-4g at 11.25.17-counter-03.png'

If you are on macOS and using tool installed via brew install rename, then following should work:

rename -N ...01 -X -e '$_ = "${_}-counter-$N"' your_files

Example:

> rename -n -N ...01 -X -e '$_ = "${_}-counter-$N"' *.png
'iPad-Pro12.9-4g at 11.24.52.png' would be renamed to 'iPad-Pro12.9-4g at 11.24.52-counter-01.png'
'iPad-Pro12.9-4g at 11.25.09.png' would be renamed to 'iPad-Pro12.9-4g at 11.25.09-counter-02.png'
'iPad-Pro12.9-4g at 11.25.17.png' would be renamed to 'iPad-Pro12.9-4g at 11.25.17-counter-03.png'
软甜啾 2024-12-24 14:35:59

perl one-liner 如果您有权访问 perl 本身

txt 文件列表进行试运行

perl -le '($old=$_) && s/^/++$n."-

perl one-liner 如果您有权访问 perl 本身

txt 文件列表进行试运行

"/eg && print for <*.txt>'

重命名

perl -le '($old=$_) && s/^/++$n."-

perl one-liner 如果您有权访问 perl 本身

txt 文件列表进行试运行

perl -le '($old=$_) && s/^/++$n."-

perl one-liner 如果您有权访问 perl 本身

txt 文件列表进行试运行

"/eg && print for <*.txt>'

重命名

"/eg && rename($old,$_) for <*.txt>'

  • perl -le 运行一行代码
  • ($old=$_) 保存旧名称
  • s/^/++$n。" -$`"/eg 进行替换 |请注意它有一个反勾号
    • /^/ 匹配乞求
    • ++$n 从 1 开始计数器
    • ."-$`" 将计数器附加到此
    • /eg 全局运行,e 用于评估
  • rename($old,$_)重命名$old =>; $_
  • for <*.txt> 循环遍历 txt 文件列表

您可以使用此模式重命名所有内容对于高级重命名,您可能还需要使用 sprintf。

屏幕截图

enter图像描述在这里

perl one-liner if you have access to perl itself

dry run over list of txt files

perl -le '($old=$_) && s/^/++$n."-

perl one-liner if you have access to perl itself

dry run over list of txt files

"/eg && print for <*.txt>'

rename

perl -le '($old=$_) && s/^/++$n."-

perl one-liner if you have access to perl itself

dry run over list of txt files

perl -le '($old=$_) && s/^/++$n."-

perl one-liner if you have access to perl itself

dry run over list of txt files

"/eg && print for <*.txt>'

rename

"/eg && rename($old,$_) for <*.txt>'

  • perl -le run a one-liner
  • ($old=$_) save the old name
  • s/^/++$n."-$`"/eg do the substitution | note that it has a back-tick
    • /^/ match the begging
    • ++$n start a counter from 1
    • ."-$`" append counter to this
    • /eg run globally and e is for evaluation
  • rename($old,$_) rename the $old => $_
  • for <*.txt> loop over list of txt file

You can rename everything with this pattern and for advanced renaming you may want to use sprintf as well.

screenshot

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文