#!/usr/bin/perl -w use strict; # Turn on short or detailed output my $verbose = 0; my $logdir = '.'; my $pattern = '_log'; # Get files to process (from original posted code) opendir( LOGS, $logdir ) or die ("Can't open $logdir: $!\n"); my @logfiles = grep( /$pattern/, readdir(LOGS) ); closedir(LOGS); # Slurp in addresses and assemble pattern: ^(aaa|bbb|ccc)$ my ($ippattern); my $ipfilelist = join ( '/', $logdir, 'ip.txt' ); open( IPLIST, $ipfilelist ) or die ("Can't open $ipfilelist: $!\n"); { my @iplist = ; chomp(@iplist); $ippattern = '^(' . join ( '|', @iplist ) . ')$'; } close(IPLIST); # Process files, incrementing results in $found{ip}{filename} my (%found); foreach my $file ( sort(@logfiles) ) { my $filename = join ( '/', $logdir, $file ); open( LOG, $filename ) or die ("Can't open $filename: $!\n"); while () { chomp; $found{$_}{$filename}++ if (m/$ippattern/); } close(LOG); } # Display results, looping through first in ip order, # then filenames in ASCII order foreach my $j ( sort( { unpack( "N", pack( "C4", split ( /\D/, $a, 4 ) ) ) <=> unpack( "N", pack( "C4", split ( /\D/, $b, 4 ) ) ) } keys(%found) ) ) { foreach my $k ( sort( keys( %{ $found{$j} } ) ) ) { print $j; if ($verbose) { print ' appeared ', $found{$j}{$k}, ' ', ( $found{$j}{$k} > 1 ? 'times' : 'time' ), ' in file '; } else { print ' is in file '; } print $k, "\n"; } } __END__ # # Contents of ip.txt, for testing 192.168.1.1 10.10.10.10 4.3.2.1 # # Contents of file1_log, for testing 1.1.1.1 4.3.2.1 10.20.30.40 192.168.1.1 10.10.10.10 # # Contents of file2_log, for testing 2.2.2.2 3.3.3.3 10.10.11.11 10.10.10.10 10.10.10.10 10.10.10.10 10.10.10.10 10.10.10.10 10.10.10.10 10.10.10.100 # # Sample of output, with $verbose = 0 4.3.2.1 is in file ./file1_log 10.10.10.10 is in file ./file1_log 10.10.10.10 is in file ./file2_log 192.168.1.1 is in file ./file1_log # # Sample of output, with $verbose = 1 4.3.2.1 appeared 1 time in file ./file1_log 10.10.10.10 appeared 1 time in file ./file1_log 10.10.10.10 appeared 6 times in file ./file2_log 192.168.1.1 appeared 1 time in file ./file1_log