2014-02-21 3 views
1

나는 파일에서 중복 라인을 모두 제거해야하지만 모든 캐릭터의 모습 무시 : 예를 들어특정 문자를 무시하면서 중복 행을 삭제하는 방법은 무엇입니까?

(),、“”。!?# 

을이 두 라인은 그래서 그들 중 하나가 삭제 될 것입니다 중복으로 간주 될 것이다 :

“This is a line。“ 
This is a line 

마찬가지로,이 세 가지 라인은 중복으로 간주 될 것이며, 하나만 남아있을 것입니다 :

This is another line、 with more words。 
“This is another line with more words。” 
This is another line! with more words! 
  • 중복 된 줄이 문서에 남아 있는지 여부는 중요하지 않습니다.
  • 중복을 제거한 후에는 줄의 순서를 변경하면 안됩니다.
  • 거의 모든 줄에는 중요한 구두점이 있지만 구두점은 다소 다를 수 있습니다. 어느 행을 보관해도 구두점이있을 수 있으므로 구두점을 최종 출력에서 ​​삭제하면 안됩니다.

일부 문자를 무시하면서 파일에서 중복 줄을 모두 삭제하려면 어떻게해야합니까?

답변

1

예를 들어, 기호를 삭제 한 다음 중복 된 것을 제거하면됩니다. 예를 들어

:

$ cat foo 
«This is a line¡» 
This is another line! with more words¡ 

Similarly, these three lines would be considered duplicates, and only one would remain: 
This is a line 

This is another line, with more words! 
This is another line with more words 

$ tr --delete '¡!«»,' < foo | awk '!a[$0]++' 
This is a line 
This is another line with more words 

Similarly these three lines would be considered duplicates and only one would remain: 

$ 

일을 할 것으로 보인다.

편집 : 귀하의 질문에서

, 그것은 문제가되지 않는 기호/문장 화성처럼 보인다. 당신은 정확해야합니다.

나는 그것을 쓸 시간이 없어하지만 난 쉬운 방법은 이미 인쇄 라인의 배열을 파일을 구문 분석하고 유지해야한다고 생각 :

for each line: 
    cleanedLine = stripFromSymbol(line) 
    if cleanedLine not in AlreadyPrinted: 
    AlreadyPrinted.push(cleanedLine) 
    print line 
1

이는 방법입니다. 정규화 된 버전의 키 배열로 수집합니다. 여기에서 정규화한다는 것은 원치 않는 문자를 모두 제거하고 공백을 제거하는 것을 의미합니다. 그런 다음 인쇄/보관할 가장 짧은 버전을 선택합니다. 그 휴리스틱 스 - 계속 유지할 것인가 - 그래서 계절에 맞게 지정되었습니다. 코드는 제작하기에 약간 간결하므로 명확하게하기 위해 코드를 살필 수 있습니다.

use utf8; 
use strictures; 
use open qw/ :std :utf8 /; 

my %tree; 
while (my $original = <DATA>) { 
    chomp $original; 
    (my $normalized = $original) =~ tr/ (),、“”。!?#/ /sd; 
    push @{$tree{$normalized}}, $original; 
    #print "O:",$original, $/;                              
    #print "N:",$normalized, $/;                             
} 

@{$_} = sort { length $a <=> length $b } @{$_} for values %tree; 

print $_->[0], $/ for values %tree; 

__DATA__ 
“This is a line。“ 
This is a line 
This is a line 
This is another line、 with more words。 
This is another line with more words 
This is another line! with more words! 

Yields-

This is another line with more words 
This is a line 
관련 문제