Найти и удалить почти все повторяющиеся строки с определенными знаками препинания

Найти и удалить почти все повторяющиеся строки с определенными знаками препинания

У меня есть текстовый файл с миллионами строк. Некоторые строки содержат одну и ту же буквенно-цифровую последовательность, но отличаются регистром и пунктуацией. Я считаю эти строки дубликатами. Я хочу удалить все дубликаты строк, содержащие точки, но оставить другую строку (эта другая строка обычно содержит знаки пунктуации, такие как подчеркивание или тире)

Вход:

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

должно сработать

Связанный контент