代码之家  ›  专栏  ›  技术社区  ›  Novum Gain

(bash)在所述零之前用“v”删除前导零

  •  0
  • Novum Gain  · 技术社区  · 2 年前

    首先,由于文件是.cbz,我无法使用“find”函数,递归两层会破坏它们,因此,我只能递归一层——这很适合我的需求。我正试图在涉及多达1000个文件的大规模范围内做到这一点。

    目前:

    Manga Name Here v004 - A Great Journey
    Manga Name Here v053 - A Greater Journey
    Manga Name Here v105 - The Best Journey
    

    预期

    Manga Name Here, Volume 4 - A Great Journey
    Manga Name Here, Volume 53 - A Greater Journey
    Manga Name Here, Volume 105 - The Best Journey
    

    我试过几种方法,但是。。我一个人想不通。我正在尝试的一个失败的主要原因是这个。

    rename 's/ v0([1-9]+)/, Volume $1/e' *v*.cbz

    我不太明白我做错了什么,我试过的不仅仅是“重命名”,但是。。我不知道该怎么做 将其重命名为 ,即使我去掉了前面的零。我很感激你的回答。提前谢谢!

    2 回复  |  直到 2 年前
        1
  •  0
  •   Gilles Quénot ticktalk    2 年前

    使用 Perl's rename (可在任何操作系统中使用):

    rename -n 's/v(\d+)/sprintf "Volume %.1d", $1/e' ./*
    

    去除 -n 当对输出感到满意时。

    已处理的文件:

    'Manga Name Here Volume 105 - The Best Journey'
    'Manga Name Here Volume 4 - A Great Journey'
    'Manga Name Here Volume 53 - A Greater Journey'
    
        2
  •  0
  •   Ed Morton    2 年前

    听起来你所需要的只是(未经测试):

    #!/usr/bin/env bash
    
    for old in *v*.cbz; do
        if [[ $old =~ (.* )v0*([1-9].*) ]]; then
            new="${BASH_REMATCH[0]}, Volume ${BASH_REMATCH[1]}"
            echo mv -- "$old" "$new"
        fi
    done