Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare files & output

Status
Not open for further replies.

vietboy505

Technical User
Feb 20, 2006
56
US
Compare two files and output the difference in a new file?

nameList.txt
Code:
| | |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
Code:
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?
 
It's a lill confusing what you exactly you are looking for?
Do you want to compare AAAAAAA*... from nameList.txt with
XXXXXXX... in checkList.txt and if the entries in nameList.txt don't exist in checkList.txt print them to a new file?

If so,
Read in file 1, split and read in the desired column as key
Read in file 2, split and read in the desired column as key

then compare if the key's match and print the one's that don't exist.
 
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.
 
Code:
#!/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.
 
This should give you a good start. Since I suspect you haven't had much exposure to regexes yet, I commented the ones I used.
Code:
my %names;
open FILE1, "< nameList.txt" or die;
while (<FILE1>) {
    $names{$1}++ if /^\s*           # Match beginning of line and optional spaces
                    (?:\|\s*){3}    # Match pattern of pipe and optional spaces
                                    # three times - don't capture text
                    (\w+)           # Capture word characters in $1
                    /x;             # x modifier - allows comments in the regex
}
close FILE1;

open FILE2, "<checkList.txt" or die;
while (<FILE2>) {
    if (/^\s*   # Match beginning of line and optional spaces
        \d+\s+  # 1 or more digits and one or more spaces
        (\w+)   # Capture word characters in $1
        /x) {
        if ($names{$1}) {
            print "$1 - match\n";
        } else {
            print "$1 - no match\n";
        }
    }
}
close FILE2;
 
file0.txt
Code:
| | |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:
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:
#!/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 ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top