Click to See Complete Forum and Search --> : [RESOLVED] Perl: compare two files & output to a new file


vietboy505
March 1st, 2006, 08:39 AM
Compare two files and output the difference in a new file?

nameList.txt

| | |AAAAAAA* | |* | |9999| |MEP | |- | |XXXXXXXXX| | |

| | |AAAAAAA* | |* | |9999| |MEP | |- | |XXXXXXXXX| | |

| | |CCCCCCC* | |* | |9999| |NAD | |- | |XXXXXXXXX| | |

| | |CCCCCC D* | |* | |9999| |BEM | |- | |XXXXXXXXX| |YYYYY|

| | |XXXXXX A* | |* | |9999| |MEP | |- | |XXXXXXXXX| | |

| | |ZZZZZZ A* | |* | |9999| |NAD | |- | |XXXXXXXXX| | |

| | |EEASAW A* | |* | |9999| |NA* | |- | |XXXXXXXXX| |YYYYY|

| | |ASCAWF W A* | |* | |9999| |ME* | |* | |XXXXXXXXX| | |

| | |XXXXXX A A* | |* | |9999| |BE* | |* | |XXXXXXXXX| | |

| | |AWSDAW* | |* | |9999| |ME* | |- | |XXXXXXXXX| | |

| | |WFCAPI A2* | |* | |9999| |MEP | |- | |XXXXXXXXX| | |



checkList.txt

1 XXXXXXX 6 6 U 1 2 3 4 5 6 1 1 P 1 0 1 0
2 AAAAAAA -12 11 Y 1 2 3 4 5 6 469.7 481.7 P 1 0 1 0
3 FASZFAS -12 -6 Z 1 2 3 4 426.4 431.7 Z 0 0 1 0
4 JJHJHGC -12 12 Y 1 2 3 4 5 6 446.5 457.6 P 1 1 1 0
5 JHGJHZA -9 -4 Z 1 2 3 4 405.6 410.7 Z 0 0 1 0
6 7843JHE -8 8 Y 1 2 3 4 5 6 446.5 457.6 P 1 0 1 0
7 NMHZJYA -12 12 Y 1 2 3 4 5 6 446.5 457.6 P 1 1 1 0
8 WFCAPI -8 8 Y 1 2 3 4 5 6 446.5 457.6 P 1 0 1 0
9 FASTYTA -8 8 Y 1 2 3 4 5 6 446.5 457.6 P 1 0 1 0
10 89QANJGA -14 -7 Z 1 2 3 405.6 410.7 Z 0 0 1 0


I was wondering how I can do this by passing nameList.txt in array, probably with AAAAAAA or ASCAWF, begin third characters.

Then grab the second file, checkList.txt, on text such as FASZFAS or NAETGNA, second column.

Then if array in checkList.txt doesn't match with nameList.txt, output that name to a new file.
This will keep on doing until the end of file and keep on appending the name to the same new file.

Functions I probably need to use is:
open()
close()
while loop
compare method

Can any one help me started?

mmetzger
March 1st, 2006, 09:57 AM
I'm not entirely sure what you're trying to do here....

If you want to actually compare the contents of files, save yourself some trouble and use CPAN modules...

http://perl.active-venture.com/lib/File/Compare.html

If you need to compare contents of the files, basically load each file into an array and loop over the entries accordingly.

vietboy505
March 1st, 2006, 11:20 AM
The Cpan compare the whole files though.

I want to compare if the text match or not.
Like the nameList.txt is after 3 character until it hit whitespace or tab.

Just grab this only: | | |CCCCCCC* and takes only CCCCCCC and ignore the rest on that line.

And in checklist.txt, just grab the second column only.
1 CCCCCCC takes only --> CCCCCCC

When finised grabbing both files, compare those two.

If column grab in checkList.txt doesn't match with name grab in nameList.txt, output that name to a new file.

mmetzger
March 1st, 2006, 01:53 PM
Ok, basically, here's the commands you need to know...

To read a file into an array (one of a bazillion ways to do this):


my $file1 = $ARGV[0];
open(FILE1, $file1) || die "Can't open $file1: $!\n";
my @file1data = <FILE1>;
close(FILE1);


To read each entry in an array and split the values according to a character:


foreach my $line (@file1data)
{
my @linedata = split(/|/, $line);
# Do something with the @array...
# Note that you can also do something like
# my ($col1, $col2, ... $coln) = split(/|/,$line)
# to get the data in named variables
}


My final hint is remember that you must use "eq" in perl to check for equality in string scalars. Using "==" will give you a positive result more than it should....

vietboy505
March 1st, 2006, 11:43 PM
#!/usr/local/bin/perl

open(FILE0, $ARGV[0]) || die "Can't open $_: $!\n";
open(FILE1, $ARGV[1]) || die "Can't open $_: $!\n";
@file0data = <FILE0>;
@file1data = <FILE1>;
close(FILE0);
close(FILE1);

@linedata=();
@line1data=();


while(@file0data)
{
@linedata = split(/|/);

}

while(@file1data)
{
@line1data = split(/\t/);
}

while(@file1data)
{
@line1data = split(/\t/);

}

print("FILE: $linedata[3] \n\n");
print("FILE2: $line1data[1] \n\n");


##check

while(@line1data)
{
if($line1data[1] eq $linedata[3])
{
#output $line1data[1] & append to fileCheck.txt
}

}


Here the code I try, it does pass the text in the file into the array, but the split doesn't work.

mmetzger
March 2nd, 2006, 02:05 AM
Save yourself some trouble - be explicit with your variables. I've found some odd behavior when relying on $_, especially if use strict is not defined.

Also, what do you mean by the split doesn't work - based on your entry, you're only going to see the last piece of data. The split I illustrated works on a per line basis. Each iteration through the loop overwrites @linedata.