Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
file.txt <root> <book></book> <name>Syndey</name> </root>
How to print the filename if it contains the string#!/usr/bin/perl -w use strict; our $path_to_dir='/home/path_to_dir/'; sub search_directory($path_to_dir) { my($listFiles) = @_; #Insert / at the end if the / is not specified $listFiles .= '/' if($listFiles !~ /\/$/); #print "The directory path is $path\n"; # Loop all the files in directory for my $checkFile (glob($listFiles.'*')) { ## Check the directory if( -d $checkFile) { ## If the file is directory, them loop again. &search_directory($checkFile); } my $count=0; open(IN,"$checkFile") or die "$!"; while(<IN>){ if ($_ =~ m/<book><\/book>/ and /<name +>*.*Sydney*.*/) { $count++; } } if($count >= 1){ print "filename:$checkFile and count : $count\ +n"; } } } &search_directory($path_to_dir);
where Sydney must be case insensitive<book></book> and also <name>Sydney</name>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Search in file
by Corion (Patriarch) on Sep 15, 2009 at 07:48 UTC |