#!/usr/bin/perl use strict; use XML::Parser; my ( $path, $pattern ) = @ARGV; die "Usage: $0 path pattern\n lists files in path that contain pattern\n" unless ( length($path) and -d $path and $pattern =~ /\S/ ); my $found_files = process_files( $path, $pattern ); print "the following files in $path contain '$pattern'\n", join( "\n", @$found_files ), "\n"; sub process_files { my ( $path, $pattern ) = @_; my @found = (); my $ignore = qr/\.(?:zip|lfa|txt) | UASTG | defines | sccpch | sms81154 | sms97767 /x; opendir( D, $path ) or die "opendir failed on $path: $!"; for my $file ( grep { -f "$path/$_" and !/$ignore/} readdir D ) { my $nfound = read_file( $path, $file, $pattern ); push @found, "$path/$file: $nfound" if ( $nfound ); } closedir D; return \@found; } sub read_file { my ( $path, $file, $pattern ) = @_; my $nfnd = 0; if ( open my $fh, "$path/$file" ) { my $xml = new XML::Parser( Handlers => { Char => sub { $nfnd++ if $_[1] =~ /$pattern/ } } ); $xml->parse( $fh ); } else { warn "open failed on $path/$file: $!\n"; } return $nfnd; }