1 2 3 4 5 6
my @zeilen_a = <$file_a>; my @zeilen_b = <$file_b>; my %zeilen_count; $zeilen_count{$_}++ for (@zeilen_a, @zeilen_b); my @zeilen_c = grep { $zeilen_count{$_} > 1 } (keys %zeilen_count);
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
open (C,">${OUTPATH}/${C}"); open (A,"<${OUTPATH}/${A}"); @file=<A>; close (A) or die "Cannot write $A file\n"; my $ll; my $zeile; $ll=1; foreach $zeile (@lfile) { chomp ($file[$ll]); $VALUE=substr($file[$ll],0,20); chomp ($VALUE); $VALUE = $VALUE * 1; open (B,"<${OUTPATH}/${B}") or die " Cannot open file $B for reading\n"; @file=<B>; close (B) or die "Cannot write $B file\n"; my $LINE; my @grep; @grep = (grep {$_ =~ /${VALUE}/} @file); $LINE = @grep; if ( $LINE eq 1 ) { chomp ($grep[0]); print C "$grep[0]"; } else { print C "$file[$ll]"; }; $ll++; };
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
open (C,">${OUTPATH}/${C}");
open (A,"<${OUTPATH}/${A}");
@file_a=<A>;
close (A) or die "Cannot write $A file\n";
my $ll;
my $zeile;
$ll=1;
foreach $zeile (@lfile)
{
chomp ($file[$ll]);
$VALUE=substr($file[$ll],0,20);
chomp ($VALUE);
$VALUE = $VALUE * 1;
open (B,"<${OUTPATH}/${B}") or die " Cannot open file $B for reading\n";
@file_b=<B>;
close (B) or die "Cannot write $B file\n";
my $LINE;
my @grep;
@grep = (grep {$_ =~ /${VALUE}/} @file);
$LINE = @grep;
if ( $LINE eq 1 )
{
chomp ($grep[0]);
print C "$grep[0]";
}
else
{
print C "$file[$ll]";
};
$ll++;
};
close (C) or die "Cannot write $C file\n";
$VALUE = $VALUE * 1;
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
#!/usr/bin/perl use strict; use warnings; my $OUTPATH=''; my $A=''; my $B=''; my $C=''; open(my $temp_fh, '<', "$OUTPATH/$B") or die("Can't open $B ($!)\n"); my @referenz=<$temp_fh>; close($temp_fh); open(my $out_fh, '>', "$OUTPATH/$C") or die("Can't open $C ($!)\n"); open(my $in_fh, '<', "$OUTPATH/$A") or die("Can't open $A ($!)\n"); MAINLOOP: while(my $line = <$in_fh>) { my $value=substr($line,0,20); $value+=0; for my $ref_line (@referenz) { if($ref_line =~ /$value/) { print $out_fh $ref_line; next MAINLOOP; } } print $out_fh $line; } close($in_fh); close($out_fh);
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
#!/usr/bin/perl use strict; use warnings; my $OUTPATH=''; my $A=''; my $B=''; my $C=''; my %index; open(my $temp_fh, '<', "$OUTPATH/$B") or die("Can't open $B ($!)\n"); MAINLOOP: while(my $line=<$temp_fh>) { while($line=~/(\d+)/gc) { my $value=$1; $value+=0; next if(exists($index{$value})); $index{$value}=$line; next MAINLOOP; } } close($temp_fh); open(my $out_fh, '>', "$OUTPATH/$C") or die("Can't open $C ($!)\n"); open(my $in_fh, '<', "$OUTPATH/$A") or die("Can't open $A ($!)\n"); while(my $line = <$in_fh>) { my $value=substr($line,0,20); $value+=0; if(exists($index{$value})) { print $out_fh $index{$value}; next; } print $out_fh $line; } close($in_fh); close($out_fh);
1
2
3
4
5
6
7
8
Datei A:
001 dataset1A
002 dataset2A
003 dataset3A
Datei B:
002 dataset2B
004 dataset4B
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
my %b_lookup; for (<$file_b>) { my ($num, $data) = split(/ /, $_, 2); $b_lookup{$num} = "$num $data"; } for (<$file_a>) { my ($num, $data) = split(/ /, $_, 2); if (exists $b_lookup{$num}) { print $file_c $b_lookup{$num}."\n"; } else { print $file_c "$_\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
#! /usr/bin/perl use strict; use warnings; use List::Util qw( first ); my $fileA = "A.txt"; my $fileB = "B.txt"; my $outfile = "C.txt"; my $criteria_size = 20; # be sure you have enough memory for this! my @reference = do { open my $fh, '<', $fileB; <$fh> }; open my $fhout, '>', $outfile or die "open($outfile,w) failed: $!\n"; open my $fhin, '<', $fileA or die "open($fileA,ro) failed: $!\n"; while ( my $lineA = <$fhin> ) { my $critA = substr( $lineA, 0, $criteria_size ); # return first line with matching reference number and leave it in @reference my $lineB = first { substr( $_, 0, $criteria_size ) == $critA } @reference; print $fhout ( $lineB ? $lineB : $lineA ); } close $fhin; close $fhout or die "close($outfile) failed: $!\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
#! /usr/bin/perl use strict; use warnings; use List::Util qw( first ); my $fileA = "A.txt"; my $fileB = "B.txt"; my $outfile = "C.txt"; my $criteria_siue = 20; # be sure you have enough memory for this! my %reference; open my $fh, '<', $fileB or die "open($fileB,ro) failed: $!\n"; # expensive in memory usage (as it uses temporary lists in memory) # %reference = map { substr( $_, 0, $criteria_size ) => $_ } <$fh>; while ( my $line = <$fh> ) { $refernce{ substr( $line, 0, $criteria_size ) } = $line; } close $fh; open my $fhout, '>', $outfile or die "open($outfile,w) failed: $!\n"; open my $fhin, '<', $fileA or die "open($fileA,ro) failed: $!\n"; while ( my $lineA = <$fhin> ) { my $critA = substr( $lineA, 0, $criteria_size); print $fhout ( exists $reference{$critA} ? $reference{$critA} : $lineA ); } close $fhin; close $fhout or die "close($outfile) failed: $!\n";
1
2
3
4
5
6
7
8
9
# Variante 1
real 0m0.286s
user 0m0.284s
sys 0m0.001s
# Variante 2
real 0m0.018s
user 0m0.011s
sys 0m0.005s
Guest PaulSo funktioniert es perfekt.
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
#!/usr/bin/perl use strict; use warnings; my %index; my $file_ref='/was/weis/ich.txt'; my $file_data='/keine/Ahnung/wo.txt'; my $file_out='/irgend/wo/hin.txt'; open(my $fh, '<', $file_ref) or die("Can't open $file_ref ($!)\n"); while(my $line=<$fh>) { chomp($line); # weiteres Aufbreiten der Zeile... # du weißt was noch gemacht werden sollte... $index{$line}=1; } close($fh); open(my $fho, '>', $file_out) or die("Can't open $file_out ($!)\n"); open(my $fhi, '<', $file_data) or die("Can't open $file_data ($!)\n"); while(my $line=<$fhi>) { chomp($line); # weiteres Aufbreiten der Zeile... # du weißt was noch gemacht werden sollte... if(exists($index{$line}) and $index{$line}) { print $fho "$line\n"; # wenn eine Zeile nicht doppelt gefunden werden darf: #delete($index{$line}); } } close($fhi); close($fho);