Compare 2 File (line and column)

时间:2015-07-28 22:51:27

标签: perl

I would like to compare two files: lines and column. Here's an example file:

-------------------------------DATAo.csv---------------------------------------

20150714 00:08:49
default, ame_a,   ame_m,     ame_uc, a     me_f,     ams_l, enced_affi,
DPA01,  fault,    fault,       fault,      fault,    fault,   1,
DPA02,       ,        ,             ,           ,    fault,   1,
DPA03,  fault,    fault,         olt,      fault,    fault,   1,
DPA01,  fault,       at,       fault,          2,    fault,   1,

The second has only two lines DATA

--------------------------------DATA1.csv----------------------------------

20150616 22:16:09
default,  ame_a,     ame_m,      ame_uc,     ame_f,    ams_l,  enced_affi,
default,  fault,     fault,      fault,      fault,    fault,       1,

Comparison line = h (that's what my code performs far)

The code should go in the DATA1.csv he skips 2 first line of the date and parameters, and takes the last line namely fault, fault, fault, fault, fault, 1, and leaving the name "default" when it has that line it goes into the file DATA0.csv he debuted comparison leaving the 2 first lines (date and parameters)

he falls tired line 1 the name of the system namely DPA01

fault, fault, fault, fault, fault, 1, 
fault, fault, fault, fault, fault, 1,

=> not different

line 2

fault, fault, fault, fault, fault, 1, 
     ,      ,      ,      , fault, 1,

=>  different

line 3

fault, fault, fault, fault, fault, 1, 
fault, fault,   olt, fault, fault, 1,

=>  different

line 4

fault, fault, fault, fault, fault, 1, 
fault,    at, fault,     2, fault, 1,

=> different

Comparing columns

the code is in DATA1.csv file and look for the name of the first parameter namely ame_a if one has the "fault" the value it returns in the file and look DATA0.csv below ame_a if there is a different value

DPA01,  fault, 
DPA02,       ,      
DPA03,  fault,  
DPA01,  fault,

In the case of species we see that the system with the name DPA2 does not value this sign that there is a difference. Then concidere the second parameter ie ame_m this one possesses the "fault" value in the file DATA1.csv retre the code back into the DATA0.csv file and see if any capital gains are equivalent to the "fault" in the case 'species

DPA01,  fault,  
DPA02,       ,  
DPA03,  fault,   
DPA01,     at,

We note that this is not equivalent, and so on. Finally he gives me a result like that.

              false,    false,       false,      false,   true,    true,

default,      ame_a,    ame_m,      ame_uc,      ame_f,   ams_l,  enced_affi,
true  DPA01,  fault,    fault,       fault,      fault,    fault,      1,
false DPA02,       ,         ,            ,           ,    fault,      1,
false DPA03,  fault,    fault,         olt,      fault,    fault,      1,
false DPA01,  fault,       at,       fault,          2,    fault,      1,

Number of different system = 3 Number of parameters = 4 the names are:

ame_a
ame_m 
ame_uc 
ame_f

As you will notice below if code is already part of the exercise and I am blocked for some days it is a probationary period. I beg your help. Thank You

use strict;
use warnings;
use File::Compare;


my %exclude;
.
my $file1 = $ARGV[0] || die "Unable to open file";
my $file2 = $ARGV[1] || die "Unable to open file";
my $txtdatei = $ARGV[2] || 'compare.csv';

my $compare = compare($file2, $file1);
open my $file, '<', $file1 or die $!;
while (<$file> ) {last if (/^\d+\s+\d{2}:\d{2}:\d{2}\s*$/)}
while (<$file>) {
chomp;
$exclude{$_}++;
}


open my $txtfh, '>', $txtdatei or die "Unable to open file";

open $file, '<', $file2 or die "Unable to open file";
while (<$file> ) {last if (/^\d+\s+\d{2}:\d{2}:\d{2}\s*$/)}
while (<$file>) {
chomp;
if($compare == 1){
print $txtfh "FALSE! $_\n" unless $exclude{$_};
}
elsif($compare == 0){
print $txtfh "True! the Files are equal \n";
}
}
close $txtfh;

1 个答案:

答案 0 :(得分:0)

Just to clarify, you want to find all columns on DATAo.csv that contain the same value, and all rows in DATAo.csv that match the last row in DATA1.csv.

It's clear from your code that you are relatively new to programming, the core ideas are there, just lacking the experience.

First of all, File::Compare probably doesn't do what you think it does, it doesn't have the ability to compare data in a CSV file in the way you want. You will have to build that logic in yourself.

Secondly, I would recommend you read up on arrays, this is a key component for solving this particular problem, have a look at;

To begin solving your problem, load your csv into a two dimensional array, usually in the format data[row][column], eg;

sudo apt-get update

You can now manipulate your array using various functions, see the examples below.

Remove any unwanted rows using splice, see http://perlmaven.com/how-to-eliminate-a-value-in-the-middle-of-an-array-in-perl:

my @DATAo;
open($fhDATAo, '<', $file) or die "Can't open $file: $!\n";
while (<$fhDATAo>) { push @DATAo, [split /,/]; }

Look at the data and perform some logic on it.

splice(@DATAo, 0, 1); # Removed the first row using splice(var, position, length)

From the above examples you should be able to load the DATA1.csv and perform the necessary operations.

If you need any further help update the source code.