Bill Bill - 3 months ago 6
Perl Question

perl comparing files to find lines that are missing from one of the files

/* format of the files:123|0|15454215|desc|11/30/2012|N|Y|
open FILE1, "/home/1.txt" or die $!;
open FILE2, "/home/2.txt" or die $!;

while (<FILE2>) {
@old_item = split(/\|/);
if (<FILE1> !~ /$old_item[0]\|$old_item[1]\|$old_item[2]/) {
print "$old_item[0]|$old_item[1]|$old_item[2]|$old_item[3]|$old_item[4]|$old_item[5]|$old_item[6]|DE|\n";

What I'm doing here is opening two files then comparing them to find any lines that are in the first and not in the second then outputting a line with some old and new info.

But what's happening is once it finds a line that isn't in the second file but is in the first it starts printing every line. What am I doing wrong and is there an easier way.


Why not use diff -u or some such? If you want a Perl solution, check out Algorithm::Diff.