Another Solaris/UNIX search question...

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
How do I search a directory tree for a string? I have a particular function being called, but don't know where it is defined. How can I search for "myfunction(*)"?
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
grep

find . -exec grep 'myfunction(*)' {} \;

As long as you want to search for myfunction(*) exaclty. :p
 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
Originally posted by: n0cmonkey
grep

find . -exec grep 'myfunction(*)' {} \;

As long as you want to search for myfunction(*) exaclty. :p

I want it to match the function name exactly, but match anything inside the ().
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
Originally posted by: Jumpem
Originally posted by: n0cmonkey
grep

find . -exec grep 'myfunction(*)' {} \;

As long as you want to search for myfunction(*) exaclty. :p

I want it to match the function name exactly, but match anything inside the ().

That's more difficult. Try:

find . -exec grep 'myfunction(' \;
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Not to sound too harsh, but if you don't know regular expressions already it'll probably take longer to create one than to search for generic 'functionname' and sort through the dups.

The egrep man page has a small section on regular expressions, but there is also the perlre, perlretut and perlfaq6 man pages that describe regular expressions. There's also an O'Reilly book or two on the subject. They're incredibly useful, especially in perl, but they're also really hard to read.
 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
Originally posted by: Nothinman
Not to sound too harsh, but if you don't know regular expressions already it'll probably take longer to create one than to search for generic 'functionname' and sort through the dups.

The egrep man page has a small section on regular expressions, but there is also the perlre, perlretut and perlfaq6 man pages that describe regular expressions. There's also an O'Reilly book or two on the subject. They're incredibly useful, especially in perl, but they're also really hard to read.

I know regular expressions enough to get by... I'm a bit rusty. My problem is how to get it to search through a directory tree; not just the current one.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
That's what find -exec does, it runs the command after exec on every file that find finds.
 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
Originally posted by: Nothinman
And you probably also want to use -type f, so it only runs on real files.

I can manage to grep a single file, and use find to find a list of files; but my syntax isn't right for combining the two.

I am trying to use "find /home/a/b/ -exec grep -n 'function' ".

 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
That's because you didn't specify the exec arguments properly. You need to use {} which is a place holder for the filename and end the command with a semi-colon, but since most shells use semi-colons to seperate commands you have to escape it, just like n0c did in his first example.
 

lansalot

Senior member
Jan 25, 2005
298
0
0
find . -name "*.c" -exec grep -il functionname {} \;

i ignores case, and l will list the filename - otherwise you'll just get the line on it returned. Not much help when you're looking for the file.