using find and egrep and then print file name where match was found

using find and egrep and then print file name where match was found

Post by Lars Clause » Sun, 06 Sep 1998 04:00:00



On Sun, 06 Sep 1998, Georgia Land verbalised:

Quote:

> using
>    find . -exec grep string {} \;

> will find the file containing string but it doesn't print the file
> name.

> is there a way to print the file name string was found in??

You'd be better of with
        find . | xargs grep string
Not only will grep show the filename, because it sees multiple files, but
it's also way faster, because grep is only invoked once.  However, beware
of files that look like arguments to grep and shells that only allow a
limited number of arguments.

-Lars

--

A *real* smart bomb would call in sick, perhaps move to another country,
changing its name in the process, open a beach bar maybe and live out its
days in safe anonymity.                          -- Barry O'Neill in rhod

 
 
 

using find and egrep and then print file name where match was found

Post by Lars Clause » Sun, 06 Sep 1998 04:00:00


On Sun, 06 Sep 1998, Michael P. Reilly verbalised:


>: using
>:   find . -exec grep string {} \;

>: will find the file containing string but it doesn't print the file
>: name.

>: is there a way to print the file name string was found in??

> If you system doesn't have the -l option, then you can do the same with:
>   find . -print | xargs grep string /dev/null |
>     sed 's/^\([^:]*\):.*/\1/' | uniq
> (you want the uniq to get rid of the duplicates filenames from the grep
> matches)

Isn't it a bit of overkill to use sed there?  How about
  find . -print | xargs grep string /dev/null | cut -d: -f1 | sort | uniq

(gotta do sort before uniq)

-Lars

--

A *real* smart bomb would call in sick, perhaps move to another country,
changing its name in the process, open a beach bar maybe and live out its
days in safe anonymity.                          -- Barry O'Neill in rhod

 
 
 

using find and egrep and then print file name where match was found

Post by Georgia La » Mon, 07 Sep 1998 04:00:00


using
        find . -exec grep string {} \;

will find the file containing string but it doesn't print the file
name.

is there a way to print the file name string was found in??

TIA

 
 
 

using find and egrep and then print file name where match was found

Post by Michael P. Reill » Mon, 07 Sep 1998 04:00:00


: using
:       find . -exec grep string {} \;

: will find the file containing string but it doesn't print the file
: name.

: is there a way to print the file name string was found in??

Most versions of grep have a -l option which prints the filename if
the pattern is found.  It would be better to use xargs if your system
has it tho:
  find . -print | xargs grep -l string

If you system doesn't have the -l option, then you can do the same with:
  find . -print | xargs grep string /dev/null |
    sed 's/^\([^:]*\):.*/\1/' | uniq
(you want the uniq to get rid of the duplicates filenames from the grep
matches)

And further, if you don't have xargs (*shudder*):
  find . -exec grep string /dev/null {} \; |
    sed 's/^\([^:]*\):.*/\1/' | uniq

        Arcege

 
 
 

using find and egrep and then print file name where match was found

Post by Charles Dem » Mon, 07 Sep 1998 04:00:00




>On Sun, 06 Sep 1998, Michael P. Reilly verbalised:


>>: using
>>:       find . -exec grep string {} \;

>>: will find the file containing string but it doesn't print the file
>>: name.

>>: is there a way to print the file name string was found in??

>> If you system doesn't have the -l option, then you can do the same with:
>>   find . -print | xargs grep string /dev/null |
>>     sed 's/^\([^:]*\):.*/\1/' | uniq
>> (you want the uniq to get rid of the duplicates filenames from the grep
>> matches)

>Isn't it a bit of overkill to use sed there?  How about
>  find . -print | xargs grep string /dev/null | cut -d: -f1 | sort | uniq

>(gotta do sort before uniq)

but the grep would have been by file, so after the cut, the filenames
would be grouped together (not sorted alphabetically), so uniq should
work just fine without the sort in this case.

Chuck Demas
Needham, Mass.
[posted and emailed]

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.

 
 
 

using find and egrep and then print file name where match was found

Post by José Nicola » Mon, 07 Sep 1998 04:00:00


and this?
find . -exec grep -l string {} /dev/null \;

> On Sun, 06 Sep 1998, Michael P. Reilly verbalised:


> >: using
> >:      find . -exec grep string {} \;

> >: will find the file containing string but it doesn't print the file
> >: name.

> >: is there a way to print the file name string was found in??

> > If you system doesn't have the -l option, then you can do the same with:
> >   find . -print | xargs grep string /dev/null |
> >     sed 's/^\([^:]*\):.*/\1/' | uniq
> > (you want the uniq to get rid of the duplicates filenames from the grep
> > matches)

> Isn't it a bit of overkill to use sed there?  How about
>   find . -print | xargs grep string /dev/null | cut -d: -f1 | sort | uniq

> (gotta do sort before uniq)

> -Lars

> --

> A *real* smart bomb would call in sick, perhaps move to another country,
> changing its name in the process, open a beach bar maybe and live out its
> days in safe anonymity.                          -- Barry O'Neill in rhod

 
 
 

using find and egrep and then print file name where match was found

Post by Michael P. Reill » Mon, 07 Sep 1998 04:00:00


: and this?
: find . -exec grep -l string {} /dev/null \;

If the grep has the -l, then you don't need the /dev/null, the option
specifies that the filename is printed (and only the filename) even
with one file.

        Arcege

 
 
 

using find and egrep and then print file name where match was found

Post by Charles Dem » Mon, 07 Sep 1998 04:00:00






>>On Sun, 06 Sep 1998, Michael P. Reilly verbalised:


>>>: using
>>>:   find . -exec grep string {} \;

>>>: will find the file containing string but it doesn't print the file
>>>: name.

>>>: is there a way to print the file name string was found in??

>>> If you system doesn't have the -l option, then you can do the same with:
>>>   find . -print | xargs grep string /dev/null |
>>>     sed 's/^\([^:]*\):.*/\1/' | uniq
>>> (you want the uniq to get rid of the duplicates filenames from the grep
>>> matches)

>>Isn't it a bit of overkill to use sed there?  How about
>>  find . -print | xargs grep string /dev/null | cut -d: -f1 | sort | uniq

>>(gotta do sort before uniq)

>but the grep would have been by file, so after the cut, the filenames
>would be grouped together (not sorted alphabetically), so uniq should
>work just fine without the sort in this case.

One other option is to use sort -u which does the uniq thing inside
the sort.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.

 
 
 

using find and egrep and then print file name where match was found

Post by Peter Samuels » Tue, 08 Sep 1998 04:00:00


  [Georgia Land]

Quote:> > is there a way to print the file name string was found in??


Quote:> You'd be better of with
>    find . | xargs grep string
> Not only will grep show the filename, because it sees multiple files,
> but it's also way faster, because grep is only invoked once.

Yeah, what he said.  And as others have noted, `grep -l' gives
filenames only, rather than matching lines, which might be what you
want.  Also note `grep -c' which gives a line count for each file.

Quote:> However, beware of files that look like arguments to grep

Not a problem, as long as "string" looks like a pattern and not an
option.  Arguments *after* "string" will always be filenames.

Quote:> and shells that only allow a limited number of arguments.

I think xargs takes care of this: it doesn't even use the shell, I
believe, and it should know about the system limit for command line
length, if there is one.  If necessary it will split the args up into
multiple grep's.

--
Peter Samuelson
<sampo.creighton.edu ! psamuels>

 
 
 

using find and egrep and then print file name where match was found

Post by Pete Houst » Thu, 10 Sep 1998 04:00:00



|using
|       find . -exec grep string {} \;
|
|will find the file containing string but it doesn't print the file
|name.
|
|is there a way to print the file name string was found in??

Yes. In the grep man page among all of the other useful information
it says:

       -l     Suppress normal output; instead print the  name  of
              each  input  file  from which output would normally
              have been printed.

Innit marvellous?

                        Pete
--

PO Box 220, Whiteknights, Reading, | Phone: +44-118-9875123 ext 7594
Berkshire, RG6 6AF, United Kingdom | Fax:   +44-118-9750203                
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
WWW: http://www.rdg.ac.uk/~spr96phh/pete.html Use lynx - you know you want to!

 
 
 

using find and egrep and then print file name where match was found

Post by Steven Lembar » Sat, 12 Sep 1998 04:00:00



> using
>         find . -exec grep string {} \;

> will find the file containing string but it doesn't print the file
> name.

> is there a way to print the file name string was found in??

you probably don't want to grep directories or device special
files for your string -- trying this in the root, for example,
will end up scanning every disk, tape, cd jukebox, internet
interface and tty you have access to for the string.  trying
it in /proc can end up maxing out the process limits.

if you don't want to waste time forking a zillion processes
on a uniprocessor then try:

        find . -type f | xargs --max-char 8192 grep -l $string;

using gnu's xargs w/ long args, assuming your shell buffers
that much.  if you have a multiprocessor and don't mind
chewing through a bit of cpu:

        find . -type f | xargs --max-line --max-proc 4 grep -l $string;

--
 Steven Lembark                                   2930 W. Palmer St.
 Workhorse Computing                             Chicago, IL  60647

---------------------------------------------------------------------
  The opinions expressed here are those of this company.
  I am the company.
---------------------------------------------------------------------

 
 
 

using find and egrep and then print file name where match was found

Post by Paul Heffne » Mon, 21 Sep 1998 04:00:00



> using
>         find . -exec grep string {} \;

> will find the file containing string but it doesn't print the file
> name.

> is there a way to print the file name string was found in??

Yeah, something many folks don't understand is how the arguments to
find are processed left-to-right stopping only when the operation or
statement
returns "false". An example:

find /usr -type d -name '*X*' -print

walks down the file system starting at /usr. Each file found is tested
thusly:

(type d): if the file is not a directory go on to the next file, else
(name '*X*'): if the file name doesn't match '*X*', next file. else,
(print):  print out the file name.

The exec directive is true if the program it runs returns a zero return
code.
The grep program returns zero if it finds the pattern it is asked to
find in
the file. That means the command:

find . -exec grep string {} \; -print

will do what you want; the print directive will emit the file name when
grep is sucessful and be bypassed otherwise.

Cheers,

 
 
 

using find and egrep and then print file name where match was found

Post by era eriksso » Mon, 21 Sep 1998 04:00:00



(Georgia Land) posted to comp.unix.shell:
 > using
 >   find . -exec grep string {} \;
 > will find the file containing string but it doesn't print the file
 > name.

grep has a fun side effect: when searching more than one file, it
+will+ report the name of the file it finds a string in. So you pass
it the name of two files, one of which is empty:

    find . -exec grep string {} /dev/null \;

Of course, you should probably also be using xargs here.

Hope this helps,

/* era */

--
Bot Bait: It shouldn't even matter whether  (`')  Just  (`')  http://www.iki
I am a resident of the State of Washington   \/ Married! \/   .fi/~era/

 
 
 

1. find command : only print the file name matches

Hi,
  I am using this find command to find a string,

 find . -name "*.h" -print -exec grep -i "stdio" {} \;

  The problem is it prints out all the .h files which make it harder to
see through, I wonder how to manipulate the command so that only the
files which contains the pattern are printed.

Thanks.

2. A thread running at same time on different CPU?

3. Matrox Mystique ands X.

4. Building shared libstdc++ on AIX

5. How, in a script, to test if an egrep found a match?

6. netatalk

7. Did I find a bug in egrep or am I misreading the man page?

8. Xfig for Linux

9. script to find altered files (actually only using find)

10. ** Using "find" to find files created today? ***

11. using find to search for Java source files and then compile then after find

12. HELP : find / -name man -print -> error No such file or dir

13. find cr using egrep