I've done somthing similar before myself. I think you want something like this:
use strict; use File::Find; # Perl script to search files for particular strings. # Recurses into subdirectories. # If you don't specify a directory to search on the command line, # it will default to the current directory. # define strings to look for here: my @Constants = ( "foo", "bar", "baz", ); my $path = $ARGV[0] || "."; my $count=0; find( sub { my $n = $File::Find::name; # search for files ending in .h or .c if ($n =~ /\.h$/ || $n =~ /\.c$/ ) { ++$count; # might want to use die instead of warn open(IN, "$n") or warn "can't open file $n : $! "; while (<IN>) { my $line = $_; for my $search_string (@Constants) { # the "i" makes the match case insensitive if ($line =~ /$search_string/i) { # print filename and line number and matching string print "$n : $. : $search_string\n"; } } } close (IN); } }, $path); print "Found matches in $count files.\n";

update: Fixed typo in original post - the path should default to the current working directory. Also, looking at Grandfather's reply, if you want a pattern like "foo" to NOT match something like "food" or "foobar", change the pattern match line to this:

if ($line =~ /\b$search_string\b/i) {

This adds "end of word boundary" around the pattern.


In reply to Re: Search thru directories of .c & .h files for @Constants by scorpio17
in thread Search thru directories of .c & .h files for @Constants by pmonk4ever

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.