Perl: split files

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
vietboy505
Forum Commoner
Posts: 53
Joined: Wed Feb 22, 2006 9:30 am

Perl: split files

Post by vietboy505 »

file0.txt

Code: Select all

| | |ABCDEFGH*                 | |*         | |9999| |MEP         | |-           | |XXXXXXXXX| | |
| | |IJKLMNOPQ*                 | |*         | |9999| |MEP         | |-           | |XXXXXXXXX| | |
| | |RSTUVWX*                 | |*         | |9999| |NAD         | |-           | |XXXXXXXXX| | |
| | |YZABCD   D*              | |*         | |9999| |BEM         | |-           | |XXXXXXXXX| |YYYYY|
| | |18ABCDE   A*              | |*         | |9999| |MEP         | |-           | |XXXXXXXXX| | |
| | |8ROAST   A*              | |*         | |9999| |NAD         | |-           | |XXXXXXXXX| | |
| | |ABCZZA   A*              | |*         | |9999| |NA*         | |-           | |XXXXXXXXX| |YYYYY|
| | |9WASHERE W A*              | |*         | |9999| |ME*         | |*           | |XXXXXXXXX| | |
| | |SEEDAR A A*              | |*         | |9999| |BE*         | |*           | |XXXXXXXXX| | |
| | |LIFE4*                  | |*         | |9999| |ME*         | |-           | |XXXXXXXXX| | |
| | |PROGRAM   A2*             | |*         | |9999| |MEP         | |-           | |XXXXXXXXX| | |
file1.txt

Code: Select all

1 8ROAST 6 6 U 1 2 3 4 5 6 1 1 P 1 0 1 0 
2 ABCZZA -12 11 Y 1 2 3 4 5 6  469.7 481.7 P 1 0 1 0 
3 RSTUVWX -12 -6 Z 1 2 3 4 426.4 431.7 Z 0 0 1 0 
4 ABCDEFGH -12 12 Y 1 2 3 4 5 6 446.5 457.6 P 1 1 1 0 
5 8RASTS -9 -4 Z 1 2 3 4 405.6 410.7 Z 0 0 1 0 
6 SWEETW -8 8 Y 1 2 3 4 5 6 446.5 457.6 P 1 0 1 0 
7 LIFE4 -12 12 Y 1 2 3 4 5 6 446.5 457.6 P 1 1 1 0 
8 RSTUVWX -8 8 Y 1 2 3 4 5 6 446.5 457.6 P 1 0 1 0 
9 569SHOULDOUTPUT Y -8 8 Y 1 2 3 4 5 6  446.5 457.6 P 1 0 1 0 
10 ABCZZA -14 -7 Z 1 2 3 405.6 410.7 Z 0 0 1 0
It print out the wrong solution.. Based on that..

from file1.txt
5,6,9 --> 8RASTS SWEETW 569SHOULDOUTPUT should output this.. but it doesn't because it's difference compare to file0.txt

Code: Select all

#!/usr/bin/perl

open(FILE0, $ARGV[0]) || die "Can't open $_: $!\n";

while (<FILE0>)
{
	chomp;
	my($column)=$_=~m!\|\s+\|\s+\|(\w+).*!ig;
	$file0{$column}++;
}
close(FILE0);

open(FILE1, $ARGV[1]) || die "Can't open $_: $!\n";

while (<FILE1>)
{
	chomp;
	my($column)=$_=~m!\d+\s+(\w+).*!ig;
	$file1{$column}++;
}
close(FILE1);

open (W,'>file_check.txt');
foreach  (keys %file0) {
            ##change from exist to not exists
	print W $_."\n" if (not exists $file1{$_});
}
close(W);
How come this doesn't output the right solution, 5 6 7 ?
I want to compare file0.txt & file1.txt...
If file1.txt text doesn't match with file0.txt
Output to a new line.. How can I fixed this?
Post Reply