RE: Find && Grep
>> If I had a complex ANDed expression, I'd just hard code it in the >> subroutine. >> >> For something simple, such as the word TABLE followed somewhere by the word >> ALIGN, you could use: >> >> my $stringToFind = 'TABLE.+ALIGN'; >> >> >> Regards, >> >> Rick >Sure, if you wanted to match VEGETABLE ALIGMENT. Carl, I'm not sure if you're kidding or serious, but just in case you are serious... If you wanted to match strictly the word table and the word align, you could use my $stringToFind = '\bTABLE\b.*\bALIGN\b'; or something to that effect. Since the variable $stringToFind uses RegEx, you can use literally any expression you can develop to find matching lines. If the expression becomes too complex, or in any way limits the flexibility, you can always go to the subroutine and place the RegEx there. In short, I believe the originally suggested code accomplished the originally desired task which was to find all files matching a specific pattern and containing specific text. Regards, Rick ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: Find && Grep
> > -Original Message- > > From: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED]]On Behalf Of > > Richard A. Evans > > Sent: Thursday, June 14, 2001 6:49 PM > > To: Sidwell, Josh; '[EMAIL PROTECTED]' > > Subject: RE: Find && Grep > > > > > > I believe this snippet of code does what you ask. > > It finds all files containing a specific string > > Since it prints line number and filename as it goes, > > there is no need to store them, as you could capture the results. I have recently written a module called fif.pm to do find & grep as part of my tkfif project (tkfif = Tk Find in Files). It also accepts a Perl RE for filename matching, will optionally recurse subdirs and will print out results in a style used by the IDEs I use. The Tk front end features Registry-persistent lists of Most-Recently-Used patterns, filename patterns and directories (using my MRUList module) and lists the results of each search in a new results window -- the next version (due out later today) will open the files in your favourite editor when you double click on them! http://prdownloads.sourceforge.net/tvlistings/tkfif_v2_0.zip There is a stand-alone Win32 exe version of tkfif (created with perl2exe) available at... http://prdownloads.sourceforge.net/tvlistings/tkfif_v2_0_Win32.zip Michael Erskine (MSEmtd) mailto:[EMAIL PROTECTED] ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: Find && Grep
Thank you for all of your help getting the recursive directory walking portion. I have been trying to adapt a portion of active states documentation on 'study' to grep through the documents that are found using the script you sent (again, thank you), but I am having trouble getting it to work Am I going about this correctly. I need to return the file names from the array that is created in your script if the file contains an instance of an arbitrary keyword(s). E.g --> 'Human Resources', 'Jobs', 'Intra1', etc. I would prefer to serach for the keyword taken from a file with a setup similar to this: Open(KEYWORDS, "keys.txt"); @keys = ; Close(KEYWORDS); while (<@ips>) { () # I don't know how to do this portion } __BEGIN__ $search = 'while (<>) { study;'; foreach $word (@words) { $search .= "++\$seen{\$ARGV} if /\\b$word\\b/;\n"; } $search .= "}"; @ARGV = @files; undef $/; eval $search; # this screams $/ = "\n"; # put back to normal input delimiter foreach $file (sort keys(%seen)) { print $file, "\n"; } __END__ Any and all help is welcome. I have searched the documentation, but I do not know enough to really begin. I am pretty familiar with grep, but I am certainly not a wiz at writing regular expressions. Thanks, Joshua Sidwell Network Engineer Lockheed Martin IMS Network Services Division 1400 S. Grand Ave Santa Ana, CA 92705 (714) 796-8383 E-mail ~ [EMAIL PROTECTED] - Email Confidentiality Notice --- The information in this email may be confidential, proprietary and/or sensitive and is intended only for use by the entity or individual to whom it is addressed. If you, the reader of this email and/or its attachments, are not the intended recipient, you are hereby notified that any dissemination, distribution, publishing, modification, storage or copying of this email or any of its attachments is strictly prohibited. If you have received this communication in error, please immediately notify Lockheed Martin IMS at [EMAIL PROTECTED] and destroy all copies of this message along with any attachments. ::-Original Message- ::From: $Bill Luebkert [mailto:[EMAIL PROTECTED]] ::Sent: Friday, June 15, 2001 9:52 AM ::Cc: '[EMAIL PROTECTED]' ::Subject: Re: Find && Grep :: :: ::Walter Torres wrote: ::> ::> > -Original Message- ::> > From: [EMAIL PROTECTED] ::> > ::[mailto:[EMAIL PROTECTED]]On Behalf Of ::> > $Bill Luebkert ::> > Sent: Thursday, June 14, 2001 6:26 PM ::> > Cc: Sidwell, Josh; '[EMAIL PROTECTED]' ::> > Subject: Re: Find && Grep ::> > ::> > ::> > "$Bill Luebkert" wrote: ::> > ::> ::> Nice little piece Bill, but I have one question (maybe 2)... ::> ::> I don't see what this is looking for? :: ::html and asp files. :: ::> I see what it is NOT looking for. ::> ::> Does this look for text inside files? :: ::No. Missed that part in the orig post. You would have to ::open each of the files in @files and look for any text or use grep ::system call before accumulating into @files or ??? :: ::> Does this look for certain file names? :: ::Yes. .html, .htm, .asp :: ::> Thanks ::> ::> Walter ::> ::> > ::> > Tested: ::> > ::> > use strict; ::> > use File::Find; ::> > ::> > my @files = (); # Array to hold resulting files ::> > my $rootdir = 'c:/temp';# Start here in this directory ::> > ::> > find (\&wanted, $rootdir); # Examine the files ::> > ::> > foreach (@files) { # print list ::> > print "$_\n"; ::> > } ::> > exit 0; ::> > ::> > sub wanted ::> > { ::> ># skip files not wanted and non-plain files ::> >return if not /\.(html|htm|asp)$/i or not -f $_; ::> ># Append path ::> >push @files, $File::Find::name; ::> >(use $_ for just filename) ::> > } ::> > __END__ :: :: ::-- :: ,-/- __ _ _ $Bill Luebkert ICQ=14439852 :: (_/ / )// // DBE Collectibles Mailto:[EMAIL PROTECTED] :: / ) /--< o // // http://dbecoll.webjump.com/ (Free Perl site) ::-/-' /___/_<_http://www.todbe.com/ ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
Re: Find && Grep
Walter Torres wrote: > > > -Original Message- > > From: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED]]On Behalf Of > > $Bill Luebkert > > Sent: Thursday, June 14, 2001 6:26 PM > > Cc: Sidwell, Josh; '[EMAIL PROTECTED]' > > Subject: Re: Find && Grep > > > > > > "$Bill Luebkert" wrote: > > > > Nice little piece Bill, but I have one question (maybe 2)... > > I don't see what this is looking for? html and asp files. > I see what it is NOT looking for. > > Does this look for text inside files? No. Missed that part in the orig post. You would have to open each of the files in @files and look for any text or use grep system call before accumulating into @files or ??? > Does this look for certain file names? Yes. .html, .htm, .asp > Thanks > > Walter > > > > > Tested: > > > > use strict; > > use File::Find; > > > > my @files = (); # Array to hold resulting files > > my $rootdir = 'c:/temp';# Start here in this directory > > > > find (\&wanted, $rootdir); # Examine the files > > > > foreach (@files) { # print list > > print "$_\n"; > > } > > exit 0; > > > > sub wanted > > { > ># skip files not wanted and non-plain files > >return if not /\.(html|htm|asp)$/i or not -f $_; > ># Append path > >push @files, $File::Find::name; > >(use $_ for just filename) > > } > > __END__ -- ,-/- __ _ _ $Bill Luebkert ICQ=14439852 (_/ / )// // DBE Collectibles Mailto:[EMAIL PROTECTED] / ) /--< o // // http://dbecoll.webjump.com/ (Free Perl site) -/-' /___/_<_http://www.todbe.com/ ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: Find && Grep
I believe this snippet of code does what you ask. It finds all files containing a specific string Since it prints line number and filename as it goes, there is no need to store them, as you could capture the results. Regards, Rick Evans __BEGIN__ use strict; # good practice use File::Find; # used for recursive file find my $filePattern = '\.(html|htm|asp)$'; # filename pattern to find my $stringToFind = 'TABLE'; # string to find in file @ARGV = qw(.) unless @ARGV; # use current dir if none provided find( \&fileInfo, @ARGV); # find the files # sub fileInfo # { # note: $_ holds the basename # $File::Find::name holds full filename return if -d; # skip directory entries return if not /$filePattern/i; # skip unwanted files open( FILE, "< $_" ) || # open the matching file return; # or don't! while ( ) { # read the file and print matching line # printf "%10d %-s\n", $., $File::Find::name if ( /$stringToFind/i ); } close( FILE ); # close the file } ## fileInfo -Original Message- I am trying to write a small script that will recursively search through a directory tree (from some arbitrary point) and return all of the files it finds that match an expression. I then want to grep the results for a specific string in each of thos files and return the file name and line number for each match. I am somewhat out of my depth in trying this. Any assistance would be helpful. ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
Re: Find && Grep
"$Bill Luebkert" wrote: > > "Sidwell, Josh" wrote: > > > > I am trying to write a small script that will recursively search through a > > directory tree (from some arbitrary point) and return all of the files it > > finds that match an expression. I then want to grep the results for a > > specific string in each of thos files and return the file name and line > > number for each match. > > > > I am somewhat out of my depth in trying this. Any assistance would be > > helpful. > > > > __Begin__ > > use strict; > > use File::Find; > > my @files = ('');# Array to hold the resulting > > files > > my $rootdir = 'c:/temp'; # Start here in this directory > > find(/\.(html|htm|asp)$/i, ($rootdir)); # Examine the files > > $#files = 999;# Set array size so that it is > > not incremented each time > > push ( @files, "$_" ); # Append the latest result > > that matches to the array > > print "@files\n"; # for debugging > > purposes, to see what is returned > > Untested: Tested: use strict; use File::Find; my @files = (); # Array to hold the resulting files my $rootdir = 'c:/temp';# Start here in this directory find (\&wanted, $rootdir); # Examine the files foreach (@files) { # print list print "$_\n"; } exit 0; sub wanted { return if not /\.(html|htm|asp)$/i or not -f $_;# skip files not wanted and non-plain files push @files, $File::Find::name; # Append path (use $_ for just filename) } __END__ -- ,-/- __ _ _ $Bill Luebkert ICQ=14439852 (_/ / )// // DBE Collectibles Mailto:[EMAIL PROTECTED] / ) /--< o // // http://dbecoll.webjump.com/ (Free Perl site) -/-' /___/_<_http://www.todbe.com/ ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
Re: Find && Grep
"Sidwell, Josh" wrote: > > I am trying to write a small script that will recursively search through a > directory tree (from some arbitrary point) and return all of the files it > finds that match an expression. I then want to grep the results for a > specific string in each of thos files and return the file name and line > number for each match. > > I am somewhat out of my depth in trying this. Any assistance would be > helpful. > > __Begin__ > use strict; > use File::Find; > my @files = ('');# Array to hold the resulting > files > my $rootdir = 'c:/temp'; # Start here in this directory > find(/\.(html|htm|asp)$/i, ($rootdir)); # Examine the files > $#files = 999;# Set array size so that it is > not incremented each time > push ( @files, "$_" ); # Append the latest result > that matches to the array > print "@files\n"; # for debugging > purposes, to see what is returned Untested: use strict; use File::Find; my @files = (); # Array to hold the resulting files my $rootdir = 'c:/temp';# Start here in this directory find (\&wanted, $rootdir); # Examine the files foreach (@file) { # print list print "$_\n"; } exit 0; sub wanted { next if not /\.(html|htm|asp)$/i or not -f $_; # skip files not wanted and non-plain files push @files, $File::Find::name; # Append path (use $_ for just filename) } -- ,-/- __ _ _ $Bill Luebkert ICQ=14439852 (_/ / )// // DBE Collectibles Mailto:[EMAIL PROTECTED] / ) /--< o // // http://dbecoll.webjump.com/ (Free Perl site) -/-' /___/_<_http://www.todbe.com/ ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users