Linux - 想要检查可能的重复目录(可能需要RegEx)

时间:2010-05-13 14:30:01

标签: regex linux bash grep

我有一个包含多个目录的目录,如下所示:

/音乐/
/音乐/ JoeBlogs-Back_In_Black - 1980年
/音乐/ JoeBlogs-Back_In_Black-(修复)-2003
/音乐/ JoeBlogs-Back_In_Black-(补发)-1987
/ Music / JoeBlogs-Thunder_Man-1947

我想要一个脚本通过并告诉我何时有“可能的”重复项,在上面的示例中,它会从目录列表中选择以下可能的重复项:

/音乐/ JoeBlogs-Back_In_Black-1980
/音乐/ JoeBlogs-Back_In_Black-(修复)-2003
/ Music / JoeBlogs-Back_In_Black-(ReIssue)-1987

1)这可能吗?
2)如果有,请帮忙!

2 个答案:

答案 0 :(得分:2)

跟进:

我通过编写以下Perl脚本来完成我需要的操作。这是我的第一个Perl脚本(我必须学习Perl来编写它 - 所以不要对我很难:)

#!/usr/bin/perl

# README
# 
# Checks a folder for Albums that are similar 
# eg : 
# Arist-Back_In_Black-(Remastered)-2001-XXX
# Artist-Back_In_Black-(Reissue)-2000-YYY
#
# Script prompts you for which one to "zz" (putting zz in front of the file name you can delete it later)
#
# CONFIG
# 
# Put your mp3 directory path in the $mp3dirpath variable
#

$mp3dirpath = '/data/downloads/MP3';

# END CONFIG


@txt= qx{ls $mp3dirpath};


sort (@txt);

$re1='.*?'; 
$re2='(?:[a-z][a-z0-9_]*)';
$re3='.*?';
$re4='((?:[a-z][a-z0-9_]*))';

$re=$re1.$re2.$re3.$re4;

$foreach_count_before=0; #Setups up counter
$foreach_count_after=1; #Setups up counter


$number_in_arry = scalar (@txt);

while ($foreach_count_before < $number_in_arry) {
                                        if ($txt[$foreach_count_before] =~ m/$re/is)
                                            { 
                                             $var1=$1;
                                             }
                                         if ($txt[$foreach_count_after] =~ m/$re/is)
                                            { 
                                             $var2=$1;
                                             }
                                         if ($var1 eq $var2)
                                            {
                                             print "-------------------------------------\n";
                                             print "$txt[$foreach_count_before] \n";
                                             print "MATCHES \n";
                                             print "\n$txt[$foreach_count_after] \n";
                                             print "Which Should I Remove? \n";
                                             print "[1] $txt[$foreach_count_before]\n";
                                             print "[2] $txt[$foreach_count_after]\n";
                                             print "[Any Other Key] Take No Action\n\n";

                                             $answer = <>;        # Get user input, assign it to the variable 
                                                if    ( $answer == "1" ) { 
                                                      print "ZZing $txt[$foreach_count_before]";
                                                      $originalfilename = $mp3dirpath . '/' . $txt[$foreach_count_before];
                                                      $newfilename = $mp3dirpath . '/' . 'zz' . $txt[$foreach_count_before];
                                                      $originalfilename = trim($originalfilename);
                                                      $newfilename = trim($newfilename);
                                                      qx(mv $originalfilename $newfilename);
                                                } 
                                                elsif ( $answer == "2" ) { 
                                                      print "ZZing $txt[$foreach_count_after]";
                                                      $originalfilename = $mp3dirpath . '/' . $txt[$foreach_count_after];
                                                      $newfilename = $mp3dirpath . '/' . 'zz' . $txt[$foreach_count_after];
                                                      $originalfilename = trim($originalfilename);
                                                      $newfilename = trim($newfilename);
                                                      print "mv $originalfilename $newfilename";
                                                      qx(mv $originalfilename $newfilename);
                                                } 
                                                else { 
                                                      print "Taking No Action"; 
                                                }

                                            }

                                           $foreach_count_before++;
                                           $foreach_count_after++;

                                        }

# SubRoutine For Trimming White Space from variables
sub trim($)
{
 my $string = shift;
 $string =~ s/^\s+//;
 $string =~ s/\s+$//;
 return $string;
}

答案 1 :(得分:0)

如果您的目录名称遵循常规结构,例如:

foo-Name_of_Interest-bar

然后你可以做一个简单的正则表达式去除“foo-”和“-bar”并进行直接比较。

如果不可能,你将不得不做一个更昂贵的模式匹配算法。也许类似于longest common sequenceLevenshtein distance。可能还有其他更合适的技术。

Bash(3.2或更高版本)中的简单匹配可能看起来像这个片段:

dir='/Music/JoeBlogs-Back_In_Black-(Remastered)-2003'
regex='^([^-]*)-([^-]*)-(.*)$'
if [[ ${BASH_REMATCH[1]} == ${prev_dir[1]} &&    #  "/Music/JoeBlogs"
      ${BASH_REMATCH[2]} == ${prev_dir[2]} ]]    #  "Back_In_Black"
then
    echo "we have a match"
fi

此代码段不显示find ... | while read ...循环或如何处理之前的条目和匹配列表。