The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
count the number of files which have a search string, but counting the file only once sudheshnaiyer UNIX for Dummies Questions & Answers 1 08-11-2007 10:50 AM
Search for Files that DONT contain a string tonydsam UNIX for Dummies Questions & Answers 11 05-02-2006 12:05 PM
Search a string from list of input files sivakumarvenkat UNIX for Dummies Questions & Answers 2 03-08-2006 02:08 PM
Search files for a string in the remote machine a_rivu UNIX for Dummies Questions & Answers 2 06-14-2005 11:31 AM
Search all files for specific string sureshy UNIX for Dummies Questions & Answers 4 03-06-2002 08:28 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-24-2008
Registered User
 

Join Date: Apr 2007
Posts: 7
What is the Best way to search files for a string??

I'm looking to seach all the files in a directory and sub-directories looking for a string. When the string is found, I want to display the filename and the entire line of that file that the string was found on.

what is the best way to do this ?? I've been playing around with awk, find, and grep with no luck. I am on HPUX 11.11

thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-24-2008
Registered User
 

Join Date: Apr 2007
Posts: 7
Ok, I'm getting closer. I found a grep command that is somewhat working.......

grep -i 'string' *

This is giving me what I need, but is only searching the present directory.

How do I get this command to also search the sub-directories ??
Reply With Quote
  #3 (permalink)  
Old 03-24-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,965
Try something like:

Code:
grep -i <pattern> `find`
Regards
Reply With Quote
  #4 (permalink)  
Old 03-24-2008
Registered User
 

Join Date: Apr 2007
Posts: 7
not working

Thanks for the response Franklin.

That code gives an error :

grep: can't open find

It thinks find is a filename.
Reply With Quote
  #5 (permalink)  
Old 03-24-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,965
Did you use backticks around find? You can also try it this way:

Code:
grep -i <pattern> ${find}
Regards
Reply With Quote
  #6 (permalink)  
Old 03-24-2008
Registered User
 

Join Date: Apr 2007
Posts: 7
I didn't use the backsticks the first time.....but received another error when using them in which I needed to CNTL C out of.

I copied my cmd prompts this time. The first command shows the grep command working on 2 test files that I have. I also have a sub-directory in /home/mark with another test file that I want it to find............

[unix] mark[426]: pwd
/home/mark
[unix] mark[427]: grep -i 'monkey' *
find1.test:this line contains the word monkey.
find2.test:this line contains the words monkey and turtle.
grep: can't open oldetc

[unix] mark[428]: grep -i 'monkey' `find`
Usage: find path-list [predicate-list]

[unix] mark[429]: grep -i 'monkey' ${find}
sh: find: Parameter not set.

[unix] mark[430]:
Reply With Quote
  #7 (permalink)  
Old 03-24-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,965
Perhaps your find version has a problem with it, try:

Code:
grep -i <pattern> `find . -name "*"`
Regards
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:33 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0