我需要另一双眼睛来告诉我这个管道和 sed 有什么问题

发布于 2025-01-09 08:30:20 字数 818 浏览 0 评论 0原文

我再次尝试重命名一些视频文件,但我的命令再次遇到问题。如果我能让另一双眼睛来看看这个并找出我没有看到的东西,我将不胜感激。

我有这样的文件;

'DARK Matter S01E03 Episode Three.mp4'

'DARK Matter S01E04 Episode Four.mp4'

等等......

我试图从名称中删除“第三集”,“第四集”等,并且我编写了这个命令,但它并没有按预期工作。

for file in *Episode*;do echo "rename \"$file\" "\"$file" | sed 's/ Episode*./.')";done

该命令的结果是;

rename "DARK Matter S01E03 Episode Three.mp4" "DARK Matter S01E03 Episode Three.mp4 | sed 's/ Episode*./.')
rename "DARK Matter S01E04 Episode Four.mp4" "DARK Matter S01E04 Episode Four.mp4 | sed 's/ Episode*./.')
rename "DARK Matter S01E05 Episode Five.mp4" "DARK Matter S01E05 Episode Five.mp4 | sed 's/ Episode*./.')

它无法识别 sed 的管道,并且我的 sed 命令本身编写不正确(我知道,我对正则表达式很糟糕)。 如果您能提供任何帮助,我将不胜感激。

Again I am trying to rename some video files, and again I am having problems with my command. If I could get another set of eyes to look at this and figure out what i'm not seeing I would be grateful.

I have files like this;

'DARK Matter S01E03 Episode Three.mp4'

'DARK Matter S01E04 Episode Four.mp4'

etc...

I am trying to remove the " Episode Three", " Episode Four" etc from the names and I wrote this command, but it's not quite working as expected.

for file in *Episode*;do echo "rename \"$file\" "\"$file" | sed 's/ Episode*./.')";done

The result of this command is;

rename "DARK Matter S01E03 Episode Three.mp4" "DARK Matter S01E03 Episode Three.mp4 | sed 's/ Episode*./.')
rename "DARK Matter S01E04 Episode Four.mp4" "DARK Matter S01E04 Episode Four.mp4 | sed 's/ Episode*./.')
rename "DARK Matter S01E05 Episode Five.mp4" "DARK Matter S01E05 Episode Five.mp4 | sed 's/ Episode*./.')

It's not recognizing the pipe to sed and my sed command itself is not written correctly (I know, i'm terrible with regular expressions).
I would appreciate any help you could give.

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

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

发布评论

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

评论(2

海拔太高太耀眼 2025-01-16 08:30:20

使用regex并从BASH_REMATCH[]数组中提取匹配的片段:

regex='(.*) Episode [^.]+(.*)'

for file in *Episode*
do
    if [[ "${file}" =~ $regex ]]
    then
        # typeset -p BASH_REMATCH
        newfile="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
        cp "${file}" "${newfile}"
    fi
done

注意:

  • regex被定义为拾取第一个句点后的任何内容(例如,.mp4.mkvsome.other.suffix
  • 取消注释 typeset -p 命令显示 BASH_REMATCH[] 数组的内容
  • 验证结果后,将

替换为 mv假设以下文件:

$ ls -l DARK*
-rw-rw----+ 1 username None 0 Feb 23 20:02 DARK\ Matter\ S01E03\ Episode\ Three.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:02 DARK\ Matter\ S01E04\ Episode\ Four.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:03 DARK\ Matter\ S01E05\ Episode\ Five.mkv

代码结果如下:

$ ls -l DARK*
-rw-rw----+ 1 username None 0 Feb 23 20:02 DARK\ Matter\ S01E03\ Episode\ Three.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:08 DARK\ Matter\ S01E03.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:02 DARK\ Matter\ S01E04\ Episode\ Four.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:08 DARK\ Matter\ S01E04.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:03 DARK\ Matter\ S01E05\ Episode\ Five.mkv
-rw-rw----+ 1 username None 0 Feb 23 20:08 DARK\ Matter\ S01E05.mkv

Using a regex and pulling the matching pieces from the BASH_REMATCH[] array:

regex='(.*) Episode [^.]+(.*)'

for file in *Episode*
do
    if [[ "${file}" =~ $regex ]]
    then
        # typeset -p BASH_REMATCH
        newfile="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
        cp "${file}" "${newfile}"
    fi
done

NOTES:

  • the regex is defined to pickup anything after the first period (eg, .mp4, .mkv, some.other.suffix)
  • uncomment the typeset -p command to display the contents of the BASH_REMATCH[] array
  • replace cp with mv once the results have been verified

Assuming the following files:

$ ls -l DARK*
-rw-rw----+ 1 username None 0 Feb 23 20:02 DARK\ Matter\ S01E03\ Episode\ Three.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:02 DARK\ Matter\ S01E04\ Episode\ Four.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:03 DARK\ Matter\ S01E05\ Episode\ Five.mkv

The code results in the following:

$ ls -l DARK*
-rw-rw----+ 1 username None 0 Feb 23 20:02 DARK\ Matter\ S01E03\ Episode\ Three.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:08 DARK\ Matter\ S01E03.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:02 DARK\ Matter\ S01E04\ Episode\ Four.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:08 DARK\ Matter\ S01E04.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:03 DARK\ Matter\ S01E05\ Episode\ Five.mkv
-rw-rw----+ 1 username None 0 Feb 23 20:08 DARK\ Matter\ S01E05.mkv
夜声 2025-01-16 08:30:20

我终于自己弄清楚了;

for file in *Episode*;do echo "rename \"$file\" \"$file\"" | sed 's/ Episode[^.]*//2';done

重命名“暗物质 S01E03 第 3 集.mp4” “暗物质 S01E03.mp4”

重命名“暗物质 S01E04 第 4 集.mp4” “暗物质 S01E04.mp4”

重命名“暗物质 S01E05 第 5 集.mp4” “暗物质 S01E05.mp4” ”

I figured it out myself finally;

for file in *Episode*;do echo "rename \"$file\" \"$file\"" | sed 's/ Episode[^.]*//2';done

rename "DARK Matter S01E03 Episode Three.mp4" "DARK Matter S01E03.mp4"

rename "DARK Matter S01E04 Episode Four.mp4" "DARK Matter S01E04.mp4"

rename "DARK Matter S01E05 Episode Five.mp4" "DARK Matter S01E05.mp4"

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