find - exec grep question

This is a discussion on find - exec grep question within the Unix and OS Discussions forums in Database and Unix Discussions category; I've been trying to search for a text sting in all files on the drive. The following works , BUT the actual file name is not returned (only the string occurence is printed). What else do I need??? find /opt/icor/fx -exec grep text string {} \; Thanks a llot, Mark...

Go Back   Database Forum > Database and Unix Discussions > Unix and OS Discussions

Database Forums

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 06-12-2007, 10:34 AM
Default find - exec grep question

I've been trying to search for a text sting in all files on the drive.
The following works , BUT the actual file name is not returned (only
the string occurence is printed). What else do I need???

find /opt/icor/fx -exec grep "text string" {} \;

Thanks a llot,
Mark

Reply With Quote
  #2  
Old 06-12-2007, 10:48 AM
Default Re: find - exec grep question

On 6/12/2007 3:34 PM, markg9-at-yahoo.com wrote:
> I've been trying to search for a text sting in all files on the drive.
> The following works , BUT the actual file name is not returned (only
> the string occurence is printed). What else do I need???
>
> find /opt/icor/fx -exec grep "text string" {} \;


find /opt/icor/fx -exec grep -H "text string" {} \;

"man grep" ist your friend.

S.
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
Reply With Quote
  #3  
Old 06-12-2007, 10:49 AM
Default Re: find - exec grep question

markg9-at-yahoo.com wrote:
> I've been trying to search for a text sting in all files on the drive.
> The following works , BUT the actual file name is not returned (only
> the string occurence is printed). What else do I need???
>
> find /opt/icor/fx -exec grep "text string" {} \;


The problem is that grep only prints the file name if more than one file
is specified. "find", as used above, will call grep for each file it finds.

With GNU grep (/use/sfw/bin/ggrep on Solaris 10), you can use its -H
option to print the file name even if only one file is specified.

A workaround for standard Solaris grep is:

grep "pattern" /dev/null

Feeding each file to a separate grep process is highly inefficient, too,
so the optimal solution would be:

find /opt/icor/fx -exec grep "text string" /dev/null {} +

With "+", find will feed groups of file names to the grep sub-process.
You still use the /dev/null hack (or ggrep -H) for the case where only
one file is found by "find", or where find's grouping would result in
the last file being found the only one on grep's command line.

mp.
--
Systems Administrator | Institute of Scientific Computing | Univ. of Vienna
| http://www.par.univie.ac.at/solaris/pca/
Patch Check Advanced | Analyze, download and install patches for Sun
Solaris
Reply With Quote
  #4  
Old 06-12-2007, 10:54 AM
Default Re: find - exec grep question

markg9-at-yahoo.com wrote:
> I've been trying to search for a text sting in all files on the drive.
> The following works , BUT the actual file name is not returned (only
> the string occurence is printed). What else do I need???
>
> find /opt/icor/fx -exec grep "text string" {} \;


If grep is given more than 1 filename to search it will print the
filename before the match.

The trick is to thus:

grep pattern /dev/null filename

so that /dev/null acts as an empty place holder.

Note you can also use "+" instead of ";" to reduce the exernal command
invocation load (see man page for find(1)).

find /path -exec grep pattern /dev/null '{}' '+'

hth
t
Reply With Quote
  #5  
Old 06-12-2007, 10:58 AM
Default Re: find - exec grep question

Stefan Naewe wrote:
> On 6/12/2007 3:34 PM, markg9-at-yahoo.com wrote:
>> I've been trying to search for a text sting in all files on the drive.
>> The following works , BUT the actual file name is not returned (only
>> the string occurence is printed). What else do I need???
>>
>> find /opt/icor/fx -exec grep "text string" {} \;

>
> find /opt/icor/fx -exec grep -H "text string" {} \;
>
> "man grep" ist your friend.


Note that -H is for the GNU or BSD grep: native Solaris grep does not
understand -H.

hth
t
Reply With Quote
  #6  
Old 06-12-2007, 11:14 AM
Default Re: find - exec grep question

markg9-at-yahoo.com wrote:
> I've been trying to search for a text sting in all files on the drive.
> The following works , BUT the actual file name is not returned (only
> the string occurence is printed). What else do I need???
>
> find /opt/icor/fx -exec grep "text string" {} \;
>


Another approach maybe. It's faster too

find /opt/icor/fx | xargs grep "text string"
Reply With Quote
  #7  
Old 06-12-2007, 11:17 AM
Default Re: find - exec grep question

markg9-at-yahoo.com writes:

>I've been trying to search for a text sting in all files on the drive.
>The following works , BUT the actual file name is not returned (only
>the string occurence is printed). What else do I need???


>find /opt/icor/fx -exec grep "text string" {} \;


find /opt/icor/fx -exec grep "text string" {} +


Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
Reply With Quote
  #8  
Old 06-12-2007, 12:59 PM
Default Re: find - exec grep question

On Jun 12, 3:34 pm, mar...@yahoo.com wrote:
> I've been trying to search for a text sting in all files on the drive.
> The following works , BUT the actual file name is not returned (only
> the string occurence is printed). What else do I need???
>
> find /opt/icor/fx -exec grep "text string" {} \;


find path/to/wherever -type f | xargs grep lists

this one doesnt even execute grep for every file found, and finds only
files.


HTH

charlie

--
..sigh

Reply With Quote
  #9  
Old 06-12-2007, 05:07 PM
Default Re: find - exec grep question

"Thommy M. Malmström" wrote:
> markg9-at-yahoo.com wrote:
>> I've been trying to search for a text sting in all files on the drive.
>> The following works , BUT the actual file name is not returned (only
>> the string occurence is printed). What else do I need???
>>
>> find /opt/icor/fx -exec grep "text string" {} \;
>>


> Another approach maybe. It's faster too


> find /opt/icor/fx | xargs grep "text string"


It's no faster than the '+' version shown, and it will fail by default
on filenames that have spaces or newlines in the name.

--
Darren Dunham ddunham-at-taos.com
Senior Technical Consultant TAOS http://www.taos.com/
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >
Reply With Quote
  #10  
Old 06-12-2007, 05:16 PM
Default Re: find - exec grep question

Darren Dunham wrote:
> "Thommy M. Malmström" wrote:
>> markg9-at-yahoo.com wrote:
>>> I've been trying to search for a text sting in all files on the drive.
>>> The following works , BUT the actual file name is not returned (only
>>> the string occurence is printed). What else do I need???
>>>
>>> find /opt/icor/fx -exec grep "text string" {} \;
>>>

>
>> Another approach maybe. It's faster too

>
>> find /opt/icor/fx | xargs grep "text string"

>
> It's no faster than the '+' version shown, and it will fail by default
> on filenames that have spaces or newlines in the name.


Well, the '+' thing is a little new to me...
Reply With Quote
Reply


Thread Tools
Display Modes



All times are GMT -4. The time now is 07:46 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Integrated by bbpixel2008 :: jvbPlugin R1013.368.1

Search Engine Friendly URLs by vBSEO 3.1.0
vB Ad Management by =RedTyger=
In an effort to better serve ads to our visitors, cookies are used on Mydatabasesupport.com. For more information, check out our Privacy Policy.