【问题标题】:Find & replace a character in filenames from terminal从终端查找和替换文件名中的字符
【发布时间】:2012-01-28 16:28:52
【问题描述】:

我正在尝试使用 ren-regexp 替换文件名中的字符,如下所示:

./ren-regexp.pl "s/_/-/g" *.jpg

我认为应该将 _ 替换为 - 在 jpgs 文件名中,但我得到的是:

./ren-regexp.pl: line 4: syntax error near unexpected token `newline'
./ren-regexp.pl: line 4: `<!DOCTYPE html>'

我也试过

perl ren-regexp.pl "s/_/-/g" *.jpg

这导致了很多错误,例如:

Bareword found where operator expected at ren-regexp.pl line 252, near "time class"
    (Do you need to predeclare time?)
Bareword found where operator expected at ren-regexp.pl line 252, near ""js-relative-date" datetime"
    (Missing operator before datetime?)
Bareword found where operator expected at ren-regexp.pl line 252, near ""2011-04-13T16:40:41-07:00" title"
    (Missing operator before title?)
Number found where operator expected at ren-regexp.pl line 252, near "April 13"
    (Do you need to predeclare April?)
Bareword found where operator expected at ren-regexp.pl line 262, near ""/msabramo/ren-regexp/tree/17026c762c41e2b88ed91bf78b63e54859b706e5" class"
    (Missing operator before class?)

我尝试使用 GitHub 页面和此处显示的示例:

Mass replace characters in filenames from terminal?

我哪里错了?单独运行“perl ren-regexp.pl”也会导致上述错误。

【问题讨论】:

  • 你确定你有一个好的下载吗?听起来你的程序很糟糕。 “ren-regexp.pl”在 250-260 行附近是什么样子的?
  • 那个脚本,从它的外观上看,相当……写得不好。甚至不使用 strict 和 warnings,而且看起来已经很老了。
  • 数百行糟糕的代码要做perl -MFile::Copy=move -Mautodie -we 'for (@ARGV { my $new = $_; $new =~ s/_/-/g; move $_, $new; }' *.jpg?
  • 这是几年前的事,但在寻找答案时唯一不断弹出的内容。
  • 好像是12年前写的,2005年最后更新了。

标签: macos perl shell terminal


【解决方案1】:

您似乎从

下载了脚本
https://github.com/msabramo/ren-regexp/blob/master/ren-regexp.pl

而不是

https://raw.github.com/msabramo/ren-regexp/master/ren-regexp.pl

前一个链接实际上会给你一个不适合传入 perl 的 HTML 文档。

或者您可以在浏览器中查看第一个链接,然后将 Perl 代码复制并粘贴到文件中(并去掉行号)。

【讨论】:

  • 你明白了。我总是这样做!
猜你喜欢
  • 1970-01-01
  • 2013-08-31
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
  • 2013-02-19
  • 2020-08-12
相关资源
最近更新 更多