LoginSignup
0
0

More than 5 years have passed since last update.

2つのファイルから重複したデータを出力する

Posted at

cat sort uniq を使う方法

cat a.txt b.txt | sort | uniq -d > tmp.txt

fgrep を使う場合

fgrep -xf a.txt b.txt > tmp.txt

DanKogaiさん流

perl -nle '$l{$_}++;END{$l{$_}>1 and print for keys %l}' a.txt b.txt > tmp.txt

素直にコード書く

#!/usr/bin/perl

sub data {
    my $file = shift;
    open(FILE, $file) or die "$!";
    my @data = ();
    while(<FILE>) {
        chomp($_);
        push @data, $_;
    }
    close(FILE);
    return @data;
}

@file1 = data("./a.txt");
@file2 = data("./b.txt");

my %data;
my @dup = grep { $data{$_} >= 2 } grep { ++$data{$_} > 1 } (@file1, @file2);
print "$_\n" for @dup;
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0