LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-18-2009, 10:51 AM   #1
homer_3
Member
 
Registered: May 2008
Posts: 99

Rep: Reputation: 15
find/grep command help


I'm having trouble doing a search through a directory on a specific files type and outputing the filename that the test is in along with the line number and text line.

Code:
grep -rn test dir
searches everything fine, but it includes all files (object files, makefiles, binaries, and source files) and I only care about certain ones.

when I try
Code:
find . -type f -path *.c -exec grep -rn V_BOOLEAN {} \;
I get just the line of text in the file where it appears and the line number. But it doesn't tell me what file it appears in. Adding -l to grep gives just the file names. Is it possible to get all 3? The file name, the line of text, and the line number like grep -rn gives?

*edit: I also tried to compile cscope, but it just complains it can't find regex.h even though it's sitting right there in /usr/include with all the other headers.

Last edited by homer_3; 09-18-2009 at 11:02 AM.
 
Old 09-18-2009, 11:06 AM   #2
mjones490
Member
 
Registered: Sep 2005
Distribution: LFS
Posts: 60

Rep: Reputation: 22
Try:

Code:
grep -rn test dir/*.c
grep may complain if there are no .c files in the root of your search (dir/). If that's the case, try:

Code:
touch dir/tmp.c ; grep -rn test dir/*.c ; rm dir/tmp.c
There may be more elegant ways to do this without creating a temporary file, but this is just a quick, off-the-top-of-my-head answer.

The problem with find is that it only calls grep with one file at the time, and grep figures "Duh! You already know the file name!"
 
Old 09-18-2009, 11:56 AM   #3
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
since you seem to be looking for things in code you might want to investigate using things like cscope, etags/ctags, other source code browsers

with regards to grep not printing the filename just add -H to greps options

Last edited by estabroo; 09-18-2009 at 11:57 AM. Reason: answer
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
using grep and find command shariefbe Linux - Newbie 5 04-01-2009 08:27 AM
Command line tool to find(1) or grep(1) faster? rsheridan6 Linux - Software 2 02-18-2006 03:50 PM
Help me to understand this command: find...| grep -e ... | xargs rm -f ROBERT483599 Red Hat 2 02-14-2006 08:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:15 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration