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
#!/usr/bin/perl use strict; use warnings; my $infile1 = 'content.csv'; my $infile2 = 'common.csv'; my $outfile = 'neu.csv'; my %hash; open(my $datei1, '<', $infile1) or die "Fehler beim Öffnen der Datei"; while(my $line = <$datei1>) { chomp $line; my ($nummer, $kuerzel, $gesetz) = split /;/, $line, 3; $hash{$kuerzel} = $gesetz; } close $datei1; open(my $datei2, '<', $infile2) or die "Fehler beim Öffnen der Datei"; open(my $neu, '>', $outfile) or die "Fehler beim Öffnen der Datei"; while(my $line = <$datei2>) { chomp $line; my ($nummer, $kuerzel, $gesetz) = split /;/, $line, 3; if (exists $hash{$kuerzel} && $hash{$kuerzel} ne $gesetz) { print $neu join(';', $kuerzel, $hash{$kuerzel}, $gesetz)."\n"; } } close $datei2; close $neu;
1 2 3 4 5 6 7 8 9 10
# iteriere über datei1 $count{ $line }++; # iteriere über datei2 $count{ $line }++; for my $key (keys %count) { if ($count{ $key } == 1) { print "Gesetz $key kommt nur in einer Datei vor\n"; } }
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#! /usr/bin/perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Sortkeys = 1; my $in1 = 'data1.txt'; my $in2 = 'data2.txt'; # fill hashes (quick and dirty!!!) my %hash1 = map { chomp; split m{;}, $_, 2 } grep { ! /^Kuerzel/ } do { open my $fh, '<', $in1; <$fh> }; my %hash2 = map { chomp; split m{;}, $_, 2 } grep { ! /^Kuerzel/ } do { open my $fh, '<', $in2; <$fh> }; # fill result hash; entries of hash2 OVERWRITE those from hash1 my %result = ( %hash1, %hash2 ); # check data print Dumper( \%hash1, \%hash2, \%result ); __END__ Content of data files * data1.txt: Kuerzel;Gesetz ABC;abc DEF;def GHI;ghi * data2.txt Kuerzel;Gesetz ABC;123 JKL;jkl Script's result (with comment of the source): $VAR1 = { 'ABC' => 'abc', 'DEF' => 'def', 'GHI' => 'ghi' }; $VAR2 = { 'ABC' => '123', 'JKL' => 'jkl' }; $VAR3 = { 'ABC' => '123', # overwritten by data from data2 'DEF' => 'def', # from data1 'JKL' => 'jkl', # from data2 'GHI' => 'ghi' # from data1 };
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#! /usr/bin/perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Sortkeys = 1; my $in1 = 'data1.txt'; my $in2 = 'data2.txt'; my %hash1 = map { chomp; split m{;}, $_, 2 } grep { ! /^Kuerzel/ } do { open my $fh, '<', $in1; <$fh> }; my %hash2 = map { chomp; split m{;}, $_, 2 } grep { ! /^Kuerzel/ } do { open my $fh, '<', $in2; <$fh> }; my %result = ( %hash1, %hash2 ); # identify uniq entries # grep all keys from hash1 which are not in hash2, and vice versa # take the resulting lists of the two greps and use their values as hash keys for a new hash my %single = map { $_ => 1 } ( grep { !exists $hash2{$_} } keys %hash1 ), ( grep { !exists $hash1{$_} } keys %hash2 ); # as alternative use two for-loops and iterate through keys of each hash and set each key individually # for my $k ( keys %hash1 ) { # $single{$k}++ if not exists $hash2{$k}; #} #for my $k ( keys %hash2 ) { # $single{$k}++ if not exists $hash1{$k}; #} print Dumper( \%hash1, \%hash2, \%result, \%single ); __END__ * result (with comments): # hash1 (from data1.txt) $VAR1 = { 'ABC' => 'abc', 'DEF' => 'def', 'GHI' => 'ghi' }; # hash2 (from data2.txt) $VAR2 = { 'ABC' => '123', 'JKL' => 'jkl' }; # result (combination of hash1 and hash2) $VAR3 = { 'ABC' => '123', 'DEF' => 'def', 'GHI' => 'ghi', 'JKL' => 'jkl' }; # single ( all uniq keys from both hashes) $VAR4 = { 'DEF' => 1, 'GHI' => 1, 'JKL' => 1 };