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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with unique command not sure what the problem is

Status
Not open for further replies.
Joined
Jun 3, 2007
Messages
84
Location
US
Hello everyone,

hoping someone could point me in the right direction/help me out. I have the following code and not sure what the heck I am doing wrong. Here is the code that I have at the moment.
I get a bunch of correct results but for some reason the script gets stuck in a never ending loop. If I move the code the other outside of the loop I get no results, so I am not sure what is going on.

Code:
#!/usr/bin/perl
use strict;
use warnings;

use File::Find;
my $DIRECTORY = "/Users/data/";
find(\&edits, $DIRECTORY);
sub edits()
{
if ( -f and /.txt$/ ) {  #Find files ending in .txt and drill down 
all sub dirs
    my $TEXT_FILE = $_; #save the results to $_;
    open MATCHING_FILE, $TEXT_FILE;
    my @all_lines = <MATCHING_FILE>; #Save 2 array
call all_lines
    close MATCHING_FILE;

    my %seen = ();
    for my $each_line ( @all_lines ) {
      if ( $each_line =~ /[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}|
password|(ssn=)/i ) { #Search for IP or password or ssn=
         $seen{$each_line}++;
         }
my @uniq = keys %seen;
print @uniq;
         }
    }
}


Results from all files in a directory
192.168.1.1
192.168.1.1
192.168.1.1
64.22.34.66
221.245.23.44
PASSWORD=FpnmRjE

what I want to do is remove the 192.168.1.1 dups from being printed along with all the other dups that show up.
 
This works for me:
Code:
use File::Find;
my $dir = './';
find(\&wanted, $dir);

sub wanted {
	if (-f && /\.txt$/) {
		my %seen;
		open FILE, "< $_" or die "Could not open $File::Find::name\n$!\n";

		foreach my $line (<FILE>) {
			if ($line =~ /(?:\d{1,3}\.){3}\d{1,3} | password= | ssn=/xi) {
				chomp $line;
				$seen{$line}++;
			}
		}
		
		print "\n$File::Find::name\n", '-' x 10, "\n";
		foreach my $line (keys %seen) {
			print "\t$line\n";
		}
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top