Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

Searching for a word within a list of files

Given a list of files, how can I find a specific word or phrase?

i.e.  something like...
find . -name myfile |grep -i evil_subroutine *

I want to look for all occurances of myfile and search all occurances for "evil_subroutine"

-Alex
0
Alex_Tong
Asked:
Alex_Tong
  • 2
1 Solution
 
biraCommented:
I would suggest you to use a script, like this:

  for i in `find . -name myfile`
  do
  echo "Processing" $i
  grep "evil_subroutine" $i
  done
0
 
jlevieCommented:
You can do it with find. If you want to find all the strings in all of the files, do:

find . -name myfile -exec grep -i evil_subroutine {} \;

Or if you want to know only what files contain the string do:

find . -name myfile -exec grep -il evil_subroutine {} \;
0
 
jlevieCommented:
That was wierd... I said "Sumbit" and only part got posted... Let's try again...

You can do it with find. If you want to find all the strings in all of the files, do:

find . -name myfile -exec grep -i evil_subroutine {} \;

Or if you want to know only what files contain the string do:

find . -name myfile -exec grep -il evil_subroutine {} \;

The standard operators for find apply so to look in C source files you might do:

find . -name "*.c" -exec grep -i evil_subroutine {} \;

Or if there isn't a pattern to the file names you can avoid grepping directories with:

find . -type f -exec grep -i evil_subroutine {} \;

See "man find" and "man grep" for more information.
0
 
Alex_TongAuthor Commented:
Fantastic answer.
Thanks jlevie
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now