特定の句読点を含むほぼ重複した行を見つけて削除する

特定の句読点を含むほぼ重複した行を見つけて削除する

数百万行のテキスト ファイルがあります。一部の行には同じ英数字シーケンスが含まれていますが、大文字と小文字、句読点が異なります。これらの行は重複していると考えられます。ピリオドを含む重複行を削除し、他の行は保持したいと思います (この他の行には通常、アンダースコアやダッシュなどの句読点が含まれます)

入力:

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

うまくいくはずだ

関連情報