在 linux/ubuntu 中重新排序名称文件

在 linux/ubuntu 中重新排序名称文件

我现在尝试使用 bash 脚本或 rename 或 sed 等命令以及正则表达式来更改一些(1 到 300)音乐的标题,但我失败了,可能是因为我是新手编程和终端命令的字。这就是我需要帮助的:

歌曲有相同的模式:
'字符串1 字符串2 字符串3.mp3'
示例:
'化物语 奥斯特 同学.mp3'
'绊物语 奥斯特 地狱之萨塔莫凯恩时代.mp3'

字符串1总是以后缀“monogatari”结尾,我认为这是搜索这段字符串的方法。
字符串2可以大写也可以不大写。
字符串3是歌曲的实际名称,名称之间可以有空格。
观察1.我认为选择两者的方法字符串1字符串3是通过字符串2, 之前是什么字符串3字符串1以及之后是什么字符串2字符串3
观测2。请记住,扩展名需要保持不变。

'预期输入' => '预期输出'

'Bakemonogatari Ost Classmate.mp3' => '同学 Bakemonogatari Ost.mp3'
'Bakemonogatari Ost Date.mp3' => '日期 Bakemonogatari Ost.mp3'
'Bakemonogatari Ost Dokuzetsu.mp3' = > 'Dokuzetsu Bakemonogatari Ost.mp3'
'Bakemonogatari Ost Kedamono.mp3' => 'Kedamono Bakemonogatari Ost.mp3'
'Hanamonogatari ost Nattoku.mp3' => 'Nattoku Hanamonogatari ost.mp3'
'Kizumonogatari ost Preface.mp3' => ' Preface Kizumonogatari ost.mp3'
'Kizumonogatari ost Reprise.mp3' => 'Reprise Kizumonogatari ost.mp3'
'Koimonogatari ost Jigoku no Sata mo Kane Shidai.mp3' => 'Jigoku no Sata mo Kane Shidai Koimonogatari ost.mp3'
'Koimonogatari ost Namakemono no Sekkubataraki.mp3' => 'Namakemono no Sekkubataraki Koimonogatari ost.mp3'
'Nisemonogatari ost Bangs.mp3' => 'Bangs Nisemonogatari ost.mp3'
'Nisemonogatari ost Story.mp3' => 'Story Nisemonogatari ost Bangs.mp3 '
'Nisemonogatari ost Unrest.mp3' => 'Unrest Nisemonogatari ost Bangs.mp3'

用户43791的分辨率:

#!/bin/bash
# You can adjust the pattern "*.mp3" to match a different file extension or a different directory if needed - user43791
for i in *.mp3; do
    # Keep the path - user43791
    p="$(dirname "$i")"

    if [[ ${i##*/} =~ ^(.*gatari)\ +([Oo]st)\ +(.*)(\.[^.]+)$ ]]; then
        # Content of BASH_REMATCH: - user43791
        # [1] from start until the end of monogatari - user43791
        # [2] Ost or ost - user43791
        # [3] Everything after ost/Ost until (and not including) the last dot - user43791
        # [4] The extension (.mp3) - user43791
    mv "$i" "$p/${BASH_REMATCH[3]} ${BASH_REMATCH[1]} ${BASH_REMATCH[2]}${BASH_REMATCH[4]}"
    fi

    #I repeated the loop for the others patterns that i finded after the response by user43791 in the files - Rodrigo

    if [[ ${i##*/} =~ ^(.*iro)\ +([Oo]st)\ +(.*)(\.[^.]+)$ ]]; then
    mv "$i" "$p/${BASH_REMATCH[3]} ${BASH_REMATCH[1]} ${BASH_REMATCH[2]}${BASH_REMATCH[4]}"
    fi
    if [[ ${i##*/} =~ ^(.*uro)\ +([Oo]st)\ +(.*)(\.[^.]+)$ ]]; then
    mv "$i" "$p/${BASH_REMATCH[3]} ${BASH_REMATCH[1]} ${BASH_REMATCH[2]}${BASH_REMATCH[4]}"
    fi
    if [[ ${i##*/} =~ ^(.*II)\ +([Oo]st)\ +(.*)(\.[^.]+)$ ]]; then
    mv "$i" "$p/${BASH_REMATCH[3]} ${BASH_REMATCH[1]} ${BASH_REMATCH[2]}${BASH_REMATCH[4]}"
    fi
    if [[ ${i##*/} =~ ^(.*lll)\ +([Oo]st)\ +(.*)(\.[^.]+)$ ]]; then
        mv "$i" "$p/${BASH_REMATCH[3]} ${BASH_REMATCH[1]} ${BASH_REMATCH[2]}${BASH_REMATCH[4]}"
    fi
    if [[ ${i##*/} =~ ^(.*eason)\ +([Oo]st)\ +(.*)(\.[^.]+)$ ]]; then
        mv "$i" "$p/${BASH_REMATCH[3]} ${BASH_REMATCH[1]} ${BASH_REMATCH[2]}${BASH_REMATCH[4]}"
    fi
done

答案1

对于bash唯一的答案,根据您的规格,您可能能够摆脱像这样简单的事情:

# You can adjust the pattern "*.mp3" to match a different file extension or a different directory if needed
for i in *.mp3; do
    # Keep the path
    p="$(dirname "$i")"

    if [[ ${i##*/} =~ ^(.*monogatari)\ +([Oo]st)\ +(.*)(\.[^.]+)$ ]]; then
        # Content of BASH_REMATCH:
        # [1] from start until the end of monogatari
        # [2] Ost or ost
        # [3] Everything after ost/Ost until (and not including) the last dot
        # [4] The extension (.mp3)
        mv "$i" "$p/${BASH_REMATCH[3]} ${BASH_REMATCH[1]} ${BASH_REMATCH[2]}${BASH_REMATCH[4]}"
    fi
done

注意:所有这些代码都未经测试,请确保在运行echo之前通过放置在前面来检查它是否有效。mv

相关内容