使用脚本在linux上使用regex重命名文件

时间:2014-11-10 04:54:31

标签: regex linux bash

我想重命名以下文件。基本上提取一年左边的单词,删除任何非字母数字字符并用空格替换它们,然后将年份放在右括号中。

Annabelle.2014.HC.HDRip.XViD.AC3-juggs[ETRG].avi
Dracula Untold 2014 720p HDRip x264 AC3-JYK.mkv
From.Paris.with.Love.2010.1080p.BluRay.x264.YIFY.mp4
Godzilla.2014.1080p.BluRay.x264.YIFY.mp4
If.I.Stay.2014.1080p.BluRay.x264-SPARKS.mkv
Into.the.Storm.2014.1080p.BluRay.x264.YIFY.mp4
Jessabelle.2014.HDRip.XViD-juggs[ETRG].avi
Ouija.2014.1080p.HC.WEBRip.x264.AAC2.0-RARBG.mp4
Teenage Mutant Ninja Turtles (2014) 1080p Full HDRip x264 AC3 6Ch-CPG.mp4
The.Purge.Anarchy.2014.1080p.BluRay.x264.YIFY.mp4
Transformers.Age.of.Extinction.2014.1080p.BluRay.x264.YIFY.mp4

所以输出应该如下:

Annabelle (2014).avi
Dracula Untold (2014).mkv
From Paris with Love (2010).mp4
Godzilla (2014).mp4
If I Stay (2014).mkv
Into the Storm (2014).mp4
Jessabelle (2014).avi
Ouija (2014).mp4
Teenage Mutant Ninja Turtles (2014).mp4
The Purge Anarchy (2014).mp4
Transformers Age of Extinction (2014).mp4

我通过例子学习,而且我很难搞清楚这一点。我会告诉你我尝试过的东西,但这太令人尴尬了。

3 个答案:

答案 0 :(得分:3)

怎么样

sed -r 'y/./ /; s/[()]//g; s/^([^0-9]+)([0-9]+).*\s([a-z0-9]+)$/\1(\2).\3/;' inputFile

输出

Annabelle (2014).avi
Dracula Untold (2014).mkv
From Paris with Love (2010).mp4
Godzilla (2014).mp4
If I Stay (2014).mkv
Into the Storm (2014).mp4
Jessabelle (2014).avi
Ouija (2014).mp4
Teenage Mutant Ninja Turtles  (2014).mp4
The Purge Anarchy (2014).mp4
Transformers Age of Extinction (2014).mp4

<强>用法

for file in *
do
   mv "$file" "$(echo "$file" | sed -r 'y/./ /; s/[()]//g; s/^([^0-9]+)([0-9]+).*\s([a-z0-9]+)$/\1(\2).\3/;')"
done

<强>测试

$ ls
Annabelle.2014.HC.HDRip.XViD.AC3-juggs[ETRG].avi  From.Paris.with.Love.2010.1080p.BluRay.x264.YIFY.mp4  Godzilla.2014.1080p.BluRay.x264.YIFY.mp4
$ for file in *; do    mv $file "$(echo $file | s sed -r 'y/./ /; s/[()]//g; s/^([^0-9]+)([0-9]+).*\s([a-z0-9]+)$/\1(\2).\3/;' )" ; done
$ ls
Annabelle (2014).avi  From Paris with Love (2010).mp4  Godzilla (2014).mp4

答案 1 :(得分:1)

我在perl程序中尝试了上面的代码。

如有必要,请使用它。

   use warnings;
   use strict;    
    opendir(DIR, "$ARGV[0]");
    my @file = grep{/\.[^\.]+$/i} readdir(DIR);
    close(DIR);     

    foreach my $songs (@file){
        my $song = $songs;
        $song =~ s{((?:(?!(?:\()?[0-9]{4}(?:\))?).)*)(?:\()?(2[0-9]{3})(?:\))?(.*?)(\.[^\.]+)$}{$1 ($2)$4}igm;

        $song =~ s{((?:(?!(?:\()?[0-9]{4}(?:\))?).)*(?:\()?2[0-9]{3}(?:\))?)(\.[^\.]+)$}{
            my $songname = $1;
            my $songtype = $2;
            $songname =~ s{\.}{ }igm;
            qq($songname$songtype)
        }isgem;

        rename("$ARGV[0]\\$songs", "$ARGV[0]\\$song") || die ( "Error in renaming" );

    }   

输出:

Annabelle  (2014).avi
Dracula Untold  (2014).mkv
From Paris with Love  (2010).mp4
Godzilla  (2014).mp4
If I Stay  (2014).mkv
Into the Storm  (2014).mp4
Jessabelle  (2014).avi
Ouija  (2014).mp4
Teenage Mutant Ninja Turtles  (2014).mp4
The Purge Anarchy  (2014).mp4
Transformers Age of Extinction  (2014).mp4

答案 2 :(得分:0)

在存储要重命名的文件的目录上尝试以下rename命令。

rename 's/\b\d{4}\b\K.+(?=\.)//g;s/[^A-Za-z\d]+(?=.*?\.[^.]+$)/ /g;s/\b(\d{4})\./($1)./' *.*

示例:

$ ls
Annabelle.2014.HC.HDRip.XViD.AC3-juggs[ETRG].avi
Teenage Mutant Ninja Turtles (2014) 1080p Full HDRip x264 AC3 6Ch-CPG.mp4
Transformers.Age.of.Extinction.2014.1080p.BluRay.x264.YIFY.mp4
$ rename 's/\b\d{4}\b\K.+(?=\.)//g;s/[^A-Za-z\d]+(?=.*?\.[^.]+$)/ /g;s/\b(\d{4})\./($1)./' -vn *.*
Annabelle.2014.HC.HDRip.XViD.AC3-juggs[ETRG].avi renamed as Annabelle (2014).avi
Teenage Mutant Ninja Turtles (2014) 1080p Full HDRip x264 AC3 6Ch-CPG.mp4 renamed as Teenage Mutant Ninja Turtles (2014).mp4
Transformers.Age.of.Extinction.2014.1080p.BluRay.x264.YIFY.mp4 renamed as Transformers Age of Extinction (2014).mp4