Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Searching for a string in text files

Searching for a string in text files
Thread Tools
Junior Member
Join Date: Nov 2003
Status: Offline
Reply With Quote
Nov 23, 2003, 04:44 AM
 
Hi all,

I am currently looking for string in file usng the following command:

find (dir) -exec grep -l 'string' {} \;

This, however, also tries to look (and succeeds, I must say) into binary files. Is there a way to make this command only look into text files?

I am thinking along filtering what find finds through 'file' and then grepping for text, but lining the commands and pipes is beyond me at this point.

Any ideas?

Many thanks in advance,
_nmk
     
Zim
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status: Offline
Reply With Quote
Nov 23, 2003, 07:24 AM
 
Not a bullet-proof suggestion, but *often* non-binary files end in .pl, .txt, etc...

grep -i foo *.*

would only search files of the format xxxx.yyy and skip most compiled binaries (which generally have no extension). The -i makes the search case insensitive.

Mike
     
Junior Member
Join Date: May 2002
Location: Germany/Heidelberg or Germany/Dresden
Status: Offline
Reply With Quote
Nov 24, 2003, 12:25 PM
 
Originally posted by _nmk:
Hi all,

I am currently looking for string in file usng the following command:

find (dir) -exec grep -l 'string' {} \;

This, however, also tries to look (and succeeds, I must say) into binary files. Is there a way to make this command only look into text files?

I am thinking along filtering what find finds through 'file' and then grepping for text, but lining the commands and pipes is beyond me at this point.

Any ideas?

Many thanks in advance,
_nmk
Perhaps the option '-type' for the find command fits to your needs. Also the command file might be useful.

Kind regards, Lars
     
Registered User
Join Date: Oct 2003
Status: Offline
Reply With Quote
Nov 24, 2003, 01:34 PM
 
You could use 'find' with 'file' and write a short script, but here's a simple one-liner:

find dir -type f \( -name '*.txt' -o -name '*.cpp' -o -name '*.hpp' \) -exec grep -l 'string' {} \;
     
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Nov 25, 2003, 05:17 AM
 
Originally posted by _nmk:
Hi all,

I am currently looking for string in file usng the following command:

find (dir) -exec grep -l 'string' {} \;

This, however, also tries to look (and succeeds, I must say) into binary files. Is there a way to make this command only look into text files?

I am thinking along filtering what find finds through 'file' and then grepping for text, but lining the commands and pipes is beyond me at this point.

Any ideas?

Many thanks in advance,
_nmk
According to `grep' man page `grep -I' treats binary files as if they contained no match... so... I'm not quite sure why you are having problems? Or what exactly is your problem
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:45 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2