2010-05-13 4 views
1

나는이 다음과 같이 여러 디렉토리를 포함하는 디렉토리 :리눅스 - (아마 필요 정규식) 가능한 중복 디렉토리에 대한 확인 할

/음악/
/음악/JoeBlogs-Back_In_Black 1980
/음악/JoeBlogs-Back_In_Black- (Remastered 작품) -2003
/음악/JoeBlogs-Back_In_Black- (재발급) -1987
/음악

내가 스크립트를 통과하고있을 때 나에게 말하고 싶어/JoeBlogs-Thunder_Man-1947 위의 예에서 '가능한'중복

/음악/JoeBlogs-Back_In_Black 1980
/음악/JoeBlogs-Back_In_Black- (Remastered 작품) -2003
/음악/JoeBlogs-Back_In_Black- (재발급 : LD 디렉토리 목록에서 다음과 같은 가능한 중복을 데리러) -1987

1) 가능합니까?
2) 그렇다면 도움주세요!

+1

당신은 여러 개의 디렉토리가 있지만 하나의 디렉토리 만 표시했다고 말합니다. 다른 디렉토리는 어떻게 생겼습니까? 'JoeBlogs-Back_In_Black-1980' 사이의 두 번째 텍스트 필드는 항상 노래 이름입니까? – dawg

답변

0

디렉토리 이름과 같은 규칙적인 구조를 따르십시오 :

foo-Name_of_Interest-bar 

다음 당신은 "foo-"과 "- 바 '를 제거하고 직접 비교를 할 수있는 간단한 정규식을 할 수 있습니다.

그럴 수 없다면 훨씬 더 비싼 패턴 일치 알고리즘을 수행해야합니다. 아마도 longest common sequence 또는 Levenshtein distance과 같은 것일 수 있습니다. 더 적절한 다른 기술이있을 수 있습니다. 배쉬에서

간단한 매칭 (버전 3.2 이상)이 조각과 같습니다이 조각은 find ... | while read ... 루프 또는 얼마나 일치의 이전 항목과 목록을 처리 할 수있는 표시되지 않습니다

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 

.

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; 
}