1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#!/usr/bin/perl
use strict; use warnings;
# usage : script fichier1 fichier2 sortie
# imprime dans sortie les lignes de fichier2 qui
# correspondent aux lignes de fichier1
open my $input1, '<', $ARGV[0]
or die "Problème pour ouvrir $ARGV[0] : $!\n";
open my $input2, '<', $ARGV[1]
or die "Problème pour ouvrir $ARGV[1] : $!\n";
open my $duplicated_lines_output, '>', $ARGV[2]
or die "Problème pour ouvrir $ARGV[2] : $!\n";
# construit une table d'association dont les clés sont
# les lignes du fichier1
my %presents_in_file;
my @lines_in_input1 = <$input1>;
@presents_in_file{ @lines_in_input1 } = (1) x @lines_in_input1;
while( my $line = <$input2> ) {
# si la ligne du fichier2 est une clé de la table d'association,
# elle était également dans le fichier1
print {$duplicated_lines_output} $line
if exists $presents_in_file{$line};
}
close($input1);
close($input2);
close($duplicated_lines_output); |
Partager