Leser: 14
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 59 60 61 62 63 64
#!/usr/bin/perl use warnings; use strict; my $report_file ; my $status_file ; my $r_Line; my $s_Line; my ($r_feld1 , $r_feld2, $r_datum1 , $r_datum2 ); my ($s_feld1 , $s_feld2, $s_datum ); my $found = 0; if( $#ARGV == -1) { print STDERR "Usage: $0 report status\n"; exit 1; } $report_file = $ARGV[0]; $status_file = $ARGV[1]; open(REPORT, '<', $report_file) or die $!; while ( $r_Line = <REPORT> ) { chomp $r_Line; # alle Leerzeilen ignorieren if( $r_Line =~ /^[\s]*$/ ) { next; } # Datei: report # feld1z2 feld2z2 11/10/16 01:45:13 11/10/16 02:45:13 ( $r_feld1 , $r_feld2, $r_datum1 , $r_datum2 ) = split("\t", $r_Line); # Spalte/Feld 4 ist gleich '-' if ( $r_datum2 eq '-' ) { $found = 0; # open(STATUS, '+>>', $status_file) or die $!; open(STATUS, '+<', $status_file) or die $!; while ( $s_Line = <STATUS> ) { chomp $s_Line; ( $s_feld1 , $s_feld2, $s_datum ) = split("\t", $s_Line); if ( $r_feld1 eq $s_feld1 && $r_feld2 eq $s_feld2 && $r_datum1 eq $s_datum ) { $found = 1; next ; } } if ( $found == 0 ) { # Kein Eintrag in Status Datei: status gefunden, daher append print STATUS $r_feld1 . "\t" . $r_feld2 . "\t" . $r_datum1 . "\n"; } close(STATUS) or die $!; } } close(REPORT) or die $!;
QuoteWie groß sind denn die Report-Dateien? Ein paar Kilobytes? Megabytes? Gigabytes?
QuoteWie viele Daten davon würden in eine Status-Datei geschrieben werden?
tie( my @status, 'Tie::File', $status_file ) or die "Cannot tie $status_file: $!";
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
#!/usr/bin/perl use strict; use warnings; # use 5.010; use List::Util qw( first ); use Tie::File; die "Usage: $0 report status\n" if 2 != @ARGV; my $report_file = shift @ARGV; my $status_file = shift @ARGV; # open report for reading open my $rfh, '<', $report_file or die "open($report_file,<) failed: $!"; # tie status file to array tie( my @status, 'Tie::File', $status_file ) or die "Cannot tie $status_file: $!"; LINE: while ( my $line = <$rfh> ) { warn "(D) read: $line\n"; # skip empty lines or with # at the start of a line next LINE if $line =~ m/^[\s]*$|^#/; chomp $line; # split into fields my ( $field1, $field2, $date1, $date2 ) = split m/\t/, $line; # no action if "date2" is not "-"; read next line next LINE if $date2 ne '-'; # prepare a string to compare from first three fields my $to_save = join "\t", $field1, $field2, $date1; # write to status file if string has not been seen yet # example : $foo = first { defined($_) } @list # first defined value in @list # $foo = first { $_ > $value } @list # first value in @list which # # is greater than $value if ( first { $_ eq $to_save } @status ) { pop @status, $to_save; } } close $rfh; __END__
pop @status, $to_save;
$to_save = pop @status;
QuoteHier werden die Datei-Daten in einem Array gespeichert ? Die Array Veränderungen werden durch 'Tie::File'in die Datei geschrieben ?
1 2 3 4 5 6 7 8 9 10 11 12
my @array = qw( a b c d c ); my $delete = 'c'; # search for index of elements which are equal to $delete my @to_delete = grep { $array[$_] eq $delete } 0 .. $#array; # delete elements from array (begin with biggest index, because we modify @array) splice( @array, $_, 1 ) for reverse @to_delete; # check what is left print $_,"\n" for @array;