Text-Processing

重命名命令以刪除子字元串

  • August 25, 2016

我正在嘗試批量重命名以下文件:

art-faculty-3_29060055362_o.jpeg  
fine-arts-division-faculty-2016-2017-5_29165851925_o.jpeg  
theatre-faculty-2016-2017-1_29132529356_o.jpeg
art-history-faculty-2016-2017-1_29060057642_o.jpeg         
music-faculty-2016-2017-1_29132523816_o.jpeg

我想將它們重命名為:

art-faculty.jpeg                           
fine-arts-division-faculty.jpeg
theatre-faculty.jpeg
art-history-faculty.jpeg  
music-faculty.jpeg

這是我到目前為止所擁有的:

rename -n -D '/faculty(.*)/g' -X -v *

這將返回:

Using expression: sub { use feature ':5.18'; s/\/faculty\(\.\*\)\/g//g; s/\.   ([^.]+)\z//x and do { push @EXT, $1; $EXT = join ".", reverse @EXT } }
'art-faculty-3_29060055362_o.jpeg' unchanged
'art-history-faculty-2016-2017-1_29060057642_o.jpeg' unchanged
'fine-arts-division-faculty-2016-2017-5_29165851925_o.jpeg' unchanged
'music-faculty-2016-2017-1_29132523816_o.jpeg' unchanged
'theatre-faculty-2016-2017-1_29132529356_o.jpeg' unchanged

**是否可以將 REGEX 與刪除 (-D) 轉換一起使用?**如果是這樣,我將如何使用它來進行上面顯示的轉換?rename如果沒有,請指出我使用 REGEX執行轉換的正確方向。

快速參考

for i in *.jpeg; do echo mv "$i" "${i%faculty*}faculty.jpeg" ; done

如果符合要求,請刪除echo以更改文件名


我係統上的 perl rename 命令只有選項-v -f -n

$ rename -n 's/faculty\K.*(?=\.jpeg)//' *.jpeg
art-faculty-3_29060055362_o.jpeg renamed as art-faculty.jpeg
art-history-faculty-2016-2017-1_29060057642_o.jpeg renamed as art-history-faculty.jpeg
fine-arts-division-faculty-2016-2017-5_29165851925_o.jpeg renamed as fine-arts-division-faculty.jpeg
music-faculty-2016-2017-1_29132523816_o.jpeg renamed as music-faculty.jpeg
theatre-faculty-2016-2017-1_29132529356_o.jpeg renamed as theatre-faculty.jpeg

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