Hello there,
I'm currently using a perl scrip that checks if every line in a file 1.csv is present in a file named 2.csv, and writes only the different lines.
The output is a file called out.csv.
What I'm trying to do is to write only the most recent lines in the file out.csv. The file 1.csv is older than the file 2.csv (2 different extracts from ActiveDirectory).
What is currently done is printing the 2 lines from 1.csv and 2.csv to the out.csv.
It's actually printing all the differences between the two files into output, and not only the newest lines.
I haven't checked yet but I think that the lines of file 1.csv (older file, the ones I'd like to drop) are printed first. But I'm not sure and haven't done enought tests. It seemed to me some of them were in some kind of random order.
I'm trying to find out how to check the lines and print only the newer ones... It seems I should do that with 2 different steps:
- running the script in order to delete the similar lines
- running an other script that will count lines and drop the first n/2 lines.
use strict;
use warnings;
my $f1 = $ENV{path_Files}."COMP/1.CSV";
my $f2 = $ENV{path_Files}."COMP/2.CSV";
my $outfile = $ENV{path_Files}."COMP/update.csv";
my %results = ();
open FILE1, "$f1" or die "Could not open file: $! \n";
while(my $line = <FILE1>){
$results{$line}=1;
}
close(FILE1);
open FILE2, "$f2" or die "Could not open file: $! \n";
while(my $line =<FILE2>) {
$results{$line}++;
}
close(FILE2);
open (OUTFILE, ">$outfile") or die "Cannot open $outfile for writing \n";
foreach my $line (keys %results) {
print OUTFILE $line if $results{$line} == 1;
}
close OUTFILE;
What do you think of that ?
Bastien