in reply to text search in a file

Update:I had the args to grep backwards
I dont know what else you'll need to do with the info, since you said that you're going to print out the filenames, if you're on a *nix system you could just:

grep -Rl <basedir> <string to macth>

grep -Rl <string to macth> <basedir>
The 'R' switch is grep recursive, and the 'l' switch is to have grep only output the filenames with matches, instead of the full lines.

I use the most powerful debugger available: print!

Replies are listed 'Best First'.
Re^2: text search in a file
by davidrw (Prior) on Aug 10, 2005 at 22:18 UTC
    other way around (man grep for the switch details):
    grep [options] PATTERN [FILE...]
Re^2: text search in a file
by kwaping (Priest) on Aug 10, 2005 at 22:22 UTC
    Unfortunately, not all greps have a recursive flag, such as /usr/bin/grep on Solaris. :( I'm forced to create my own recursive grep like this:
    grep -li search_term * */* */*/* */*/*/* (etc.)
    But I agree that a recursive grep is the best solution for the OP.

      Nah - much easier to do something like:

      grep -li search_term `find . -type f`
      Assuming there aren't so many files that you exhaust the command shell line length. Or you can check out xargs, or even -exec option on find.

      TMTOWTDI - even in shell. :-)

        Alas, in addition to having no recursive option in /usr/bin/grep, Solaris also offers the inability to use /usr/bin/find and /usr/bin/xargs like this:
        find . -print0 -type f | xargs -0 grep -li search_term
        at least, not in SunOS 5.8 -- I don't know if this has been fixed in more recent releases, but if it hasn't... well, I just don't understand what's wrong with those people at Sun.

        Solaris users generally benefit from having the GNU tools installed in /usr/local/bin, and putting that in front of /usr/bin in their shell PATH.

        While we're on shell stuff, I've got to point out that that doesn't work if there's a lot of files below your current working directory. It'll fail with "argument list too long". Much better is:
        $ find . -type f -exec grep -qi search_term {} \; -print
        cheers

        davis
        Kids, you tried your hardest, and you failed miserably. The lesson is: Never try.

      Or if you use the most spiffy zsh you can do this:

      grep -li search **/*(.)

      Which will recursively search only files. But again that may bump into a number of arguments limitation (in which case you'd use print -N **/*(.) | xargs grep -li search or the like).

      --
      We're looking for people in ATL

Re^2: text search in a file
by goosefairy (Novice) on Aug 10, 2005 at 22:07 UTC

    Sheesh. *cough*

    Boy is *my* face red.

    I was so caught up in using perl I completely forgot about grep.
    That does exactly what I need.

    Thanks muchly.