Ok, not the cleanest code by any stretch, but it works.
usage: greplike.pl C:\ .c /*
will then look through all Source code on C for the beginnings of comments.
One of the little features that I like is that if you have
a file name with more than one
"." in it ( ".htm.txt") it will grab only the last part after the extension for examination.
Could be made better so it searchs across multiple lines, recognizes and strips "" from the string
you are searching for unless you escape them, and so on. Fun little program to make.
Update
Corrected my two errors that
Ovid pointed out.
Thanks Ovid! :)
use File::Find;
use strict;
use integer;
my $filecounter = 0;
my $dir = @ARGV[0];
@ARGV[1] =~ m/(.*)(\.){1}(\w+)$/g;
my $filename = $1;
my $filesoftype = $3;
my $stringtosearchfor = @ARGV[2];
print "Looking for files of type $filesoftype in $dir containing $stri
+ngtosearchfor\n";
sleep 10;
find(\&lookingfor, $dir);
print "Found $filecounter files of type $filesoftype!\n";
sub lookingfor()
{
if (($_ =~ m/\.+($filesoftype)+$/io) && (-f $_))
{
$filecounter++;
open INPUT, "<$_" || die "Unable to open $_ for examination: $
+!\n";
while(my $line = <INPUT>)
{
chomp $line;
if ($line =~ m/$stringtosearchfor/gio)
{
print "Found match in $File::Find::name at line $.\.\n
+";
}
}
close INPUT;
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.