LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-06-2010, 12:12 PM   #1
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Fedora 9
Posts: 192

Rep: Reputation: 30
Question how to make grep to search a pattern in only specific file type


Hello there;
To search a string pattern in all files in a directory and subdirectories, I am using;
Code:
grep -R "myclass::my-func(" mydirectory/
Now I want grep, to search in only specific file types say *.cc. Please help me. I have read manual of grep, but could not deduce any hint.
Best Regards.
 
Old 03-06-2010, 12:21 PM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
find mydirectory/ -name "*.cc" -type f -exec grep -l "myclass::my-func(" {} \;
 
Old 03-06-2010, 01:46 PM   #3
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Fedora 9
Posts: 192

Original Poster
Rep: Reputation: 30
Thanks troop,
The result of your command is exactly what I wanted. But I will take time to understand the command..
 
Old 03-06-2010, 06:59 PM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,053

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
Troop is right, but this enhancement is quite useful:

Code:
find mydirectory/ -name "*.cc" -type f -exec grep -lH "myclass::my-func(" {} \;
It'll show you the name of the file this string was found in.

jlinkels
 
Old 03-07-2010, 01:27 AM   #5
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Fedora 9
Posts: 192

Original Poster
Rep: Reputation: 30
Thanks jlinkels, but I see no difference;

Code:
[mq15@localhost ns234]$ find ns-allinone-2.34/ -name "*.cc" -type f -exec grep -lH "::purge(" {} \;
ns-allinone-2.34/ns-2.34/queue/rtqueue.cc
ns-allinone-2.34/ns-2.34/aodv/aodv_rqueue.cc
ns-allinone-2.34/ns-2.34/aomdv/aomdv_rqueue.cc
[mq15@localhost ns234]$ find ns-allinone-2.34/ -name "*.cc" -type f -exec grep -l "::purge(" {} \;
ns-allinone-2.34/ns-2.34/queue/rtqueue.cc
ns-allinone-2.34/ns-2.34/aodv/aodv_rqueue.cc
ns-allinone-2.34/ns-2.34/aomdv/aomdv_rqueue.cc
 
Old 03-07-2010, 05:22 AM   #6
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
Quote:
Grep will list the filenames by default if more than one filename is given. The -H option makes it do that even if only one filename is given. In both your examples, more than one filename is given.
(c) http://stackoverflow.com/questions/1...ent-situations
I think in this case(when -l presented), this option is useless.
Probably he meant:
Code:
find ns-allinone-2.34/ -name "*.cc" -type f -exec grep -H "::purge(" {} \;
output: filename: string
or
Code:
find ns-allinone-2.34/ -name "*.cc" -type f -exec grep -n "::purge(" {} \;
output: filename:№ of string: string

Last edited by troop; 03-07-2010 at 05:37 AM.
 
Old 03-07-2010, 07:02 AM   #7
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,053

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
Yes, -l already lists matching files, and -H lists the file name in addition to the matching string.

My preference is for -H as often I make mistakes in the regular expression so I wnat to see the matching string and I want to see in which file it is found.

-l als suffices in many cases of course.

jlinkels
 
Old 03-07-2010, 09:41 AM   #8
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Fedora 9
Posts: 192

Original Poster
Rep: Reputation: 30
Thanks to both of guys. Really useful information.
Have a nice day
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using "OR" type matching pattern with grep command riteshanand40 Linux - Newbie 3 05-06-2009 04:48 AM
Bash script to search for specific pattern? jimyg10 Programming 22 05-18-2007 01:02 PM
How to use grep to search for a specific variable.. memo007 Linux - Software 4 02-24-2007 05:52 PM
Grep pattern first line of a file ericcarlson Linux - Newbie 11 07-20-2004 10:51 AM


All times are GMT -5. The time now is 02:21 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration