Linux

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

  • March 27, 2021

我現在正在嘗試使用 bash 腳本或 rename 或 sed 等命令以及正則表達式來更改一些(1 到 300 首)音樂的標題,但我失敗得很慘,可能是因為我是新手程式和終端命令的單詞。這就是我需要幫助

的: 歌曲具有相同的模式:

String1 String2 String3 .mp3 '

範例:

Bakemonogatari Ost Classmate .mp3'

Kizumonogatari ost Jigoku no Sata mo Kane Shidai .mp3’ String1

將始終以後綴 ’ 結尾monogatari’,我認為這是搜尋這段字元串的方法。 這
String2可以大寫也可以不大寫。

String3是歌曲的實際名稱,名稱之間可以有空格。

觀察1。我認為選擇String1String3的方法是通過String2 ,在String3之前是String1 ,在String2之後是String3

Obs2。請記住,副檔名需要保持不變。

‘預期輸入’ => ‘預期輸出’

‘Bakemonogatari Ost Classmate.mp3’ => ‘Classmate Bakemonogatari Ost.mp3’

‘Bakemonogatari Ost Date.mp3’ => ‘Date 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

’ 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’’

‘戀物語 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

對於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

注意:所有這些程式碼都未經測試,請確保在執行之前通過 put echobefore檢查它是否有效mv

引用自:https://unix.stackexchange.com/questions/641232