Thread Textdateien vergleichen und duplikate löschen (3 answers)
Opened by BadBunny at 2012-12-13 16:10

Linuxer
 2012-12-13 17:01
#164205 #164205
User since
2006-01-27
3881 Artikel
HausmeisterIn

user image
Womit wird die Emailadresse vom Rest der Zeile getrennt?
Sollen ungleiche Zeilen aus beiden Dateien in die Ergebnisdatei?


Hier mal ein ungetesteter Code, wie ich Dich verstanden habe:

Code (perl): (dl )
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;

# file paths; input for reading, output for writing
my $input1 = 'input1.txt';
my $input2 = 'input2.txt';
my $output = 'output.txt';

# open files; input for reading, output for writing
open my $in1fh, '<', $input1 or die "open($input1,ro) failed: $!\n";
open my $in2fh, '<', $input2 or die "open($input2,ro) failed: $!\n";
open my $outfh, '>', $output or die "open($output,w)  failed: $!\n";

# read linewise from file input1
while ( my $line1 = <$in1fh> ) {
    # read line from file input2
    my $line2 = <$in2fh>;

    # extract email adresses from both lines
    # email is at the beginning of each line and separated with ';' from the rest of the line
    # user@example.org;additional data ...
    my $email1 = ( split m{;}, $line1, 2 )[0];
    my $email2 = ( split m{;}, $line2, 2 )[0];

    # if emails differ, write to output file
    if ( $email1 ne $email2 ) {
        print $outfh, $line1, $line2;
    }
}
# end of file of input1 reached


# if there are lines left in input2, read them linewise and write them to output
while ( my $line2 = <$in2fh> ) {
    print $outfh $line2;
}

# close all files
close $outfh   or die "close($output) failed: $!\n";
close $in1fh   or die "close($input1) failed: $!\n";
close $in2fh   or die "close($input2) failed: $!\n";

Last edited: 2012-12-13 17:20:09 +0100 (CET)
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread Textdateien vergleichen und duplikate löschen