Maybe your perl code is right. The perl code and the grep are doing two different things. The perl code is populating a hash, which means that you will get collisions if the same key (job number) is inserted twice. The grep, however, is less picky. Duplicates will get printed out.
That might print out something a bit closer to perl, assuming there is no other data on the line. Or, in perl, try this:grep insert_job | sort -u | wc -l
This will tell you about any dupes (not the original line, just the additional lines - we could add that, too, but I'll leave it to you if you decide you want to do that). And then, if you total the count in the file plus the dupes, you'll get what your original grep count is.my $dupes = 0; while (<FH1>) { chomp; my ($var1,$var2) = split(/:/,$_); $var2 = substr($var2,1); #remove 1st space results from substr # here is the important bit: if (exists $diffHash1{$var2}) { ++$dupes; print "Dupe on line $.: $var2\n"; } $diffHash1{$var2} = $var2; } print "$dupes dupes found in file1.\n";
That's not to say that the rest of your code is clean and doesn't require any stylistic changes, but we'll focus on the problem first, and worry about style later ;-)
In reply to Re: Different counts between perl and grep
by Tanktalus
in thread Different counts between perl and grep
by herda05
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |