특정 구두점이 있는 중복된 줄 근처를 찾아서 삭제하세요.

특정 구두점이 있는 중복된 줄 근처를 찾아서 삭제하세요.

수백만 줄의 텍스트 파일이 있습니다. 일부 줄에는 동일한 영숫자 시퀀스가 ​​포함되어 있지만 대소문자와 구두점이 다릅니다. 나는 이 줄이 중복된 것으로 간주합니다. 마침표가 포함된 중복 줄을 제거하고 다른 줄은 유지하고 싶습니다. (이 다른 줄에는 일반적으로 밑줄이나 대시와 같은 구두점이 포함됩니다.)

입력:

000
111
12_34
12.34
123-456-789
123.456.789
A.B.C
a_b_c
qwerty
qwertx
abcdefghijklm.nopqrstuvwxy.z
a-B-cdeFghiJklmNopqRStuvwxy__Z
22.2
33.3

원하는 출력:

000
111
12_34
123-456-789
a_b_c
qwerty
qwertx
a-B-cdeFghiJklmNopqRStuvwxy__Z
22.2
33.3

답변1

중복된 값이 연속되어 있다고 가정합니다!


작업을 수행하는 Perl 스크립트입니다.

큰 파일에서는 테스트되지 않았습니다!

#!/usr/bin/perl
use strict;
use warnings;

my $file = 'file1'; # path to input file
# read the input file in memory
open my $F, '<', $file or die "unable to open '$file': $!";
my @list = <$F>;chomp @list;
# delete all . - _ from each line and add this new string in the array for comparison
my @res = map {my $tmp=$_; tr/._-//d; [lc$_,$tmp] } @list;
# memoize the first values
my $prev_tst = $res[0][0];  # contains the string without punctuation
my $prev_orig = $res[0][1]; # contains original string
# loop on other values
for my $ind (1 .. @res-1) {
    my ($tst, $orig) = ($res[$ind][0], $res[$ind][1]);
    # te string without punctuation is the same as the previous
    if ($tst eq $prev_tst) {
        # if the previous original value contains dot
        if ($prev_orig =~  tr/.//) {
            # delete it
            undef $res[$ind-1];
        # if the current original value contains dot
        } elsif ($orig =~ tr/.//) {
            # delete it
            undef $res[$ind];
        }
    }
    # memorize value for next step
    $prev_tst = $tst;
    $prev_orig = $orig;
}
# write result to result file
my $result = 'result_file'; # path to result file
open my $R, '>', $result or die "unable to open '$result': $!";

for (@res) {
    next unless defined $_; # skip undifned values
    print $R $_->[1],"\n";
}


답변2

뭔가 라인을 따라

sed 's/\./-/g; s/__*/-/g' /path/to/infile | sort -u > /path/to/outfile

트릭을 수행해야합니다

관련 정보