find *.pl and grep for a string

find *.pl and grep for a string

Post by Carlos Ree » Thu, 12 Mar 1998 04:00:00



Hi gang!

I need to find all my files that have  an extention pl  ex *.pl
and find out if these files contains a string date

I was thinking in something like this:

find  . -name "*.pl" -exec grep date {} \;

but it doesnt work cause I lost the name of my files

any ide guys

Carlos Reed, from beautiful and lovely Quebec

ciao

 
 
 

find *.pl and grep for a string

Post by Lawson Hans » Fri, 13 Mar 1998 04:00:00



>I need to find all my files that have  an extention pl  ex *.pl
>and find out if these files contains a string date
>I was thinking in something like this:
>find  . -name "*.pl" -exec grep date {} \;
>but it doesnt work cause I lost the name of my files

The "grep" command will prepend the file name on any matching lines
if you give it more than one file name.

What I usually do is add a reference to "/dev/null",
so your command simply becomes:

    find  . -name "*.pl" -exec grep date {} /dev/null \;

I hope that may help.

Best regards,

Lawson Hanson

 
 
 

find *.pl and grep for a string

Post by Tapani Tarvaine » Fri, 13 Mar 1998 04:00:00




> >I need to find all my files that have  an extention pl  ex *.pl
> >and find out if these files contains a string date
> The "grep" command will prepend the file name on any matching lines
> if you give it more than one file name.

> What I usually do is add a reference to "/dev/null",
> so your command simply becomes:

>     find  . -name "*.pl" -exec grep date {} /dev/null \;

That's good advice (and often-repeated, I might add).
But if you only want a list of files that contain the string
without seeing all the lines it occurs in, you can use "-l"
option with grep:

find  . -name "*.pl" -exec grep -l date {} \;

or somewhat faster

find  . -name "*.pl" | xargs grep -l date

--
Tapani Tarvainen

 
 
 

find *.pl and grep for a string

Post by Veksler Micha » Fri, 13 Mar 1998 04:00:00




>  >I need to find all my files that have  an extention pl  ex *.pl
>  >and find out if these files contains a string date
>  >I was thinking in something like this:
>  >find  . -name "*.pl" -exec grep date {} \;
>  >but it doesnt work cause I lost the name of my files
>  The "grep" command will prepend the file name on any matching lines
>  if you give it more than one file name.
>  What I usually do is add a reference to "/dev/null",
>  so your command simply becomes:
>      find  . -name "*.pl" -exec grep date {} /dev/null \;

That will work, but will be damn slow if you have lots of files.
Faster wy would be something like:
  find - name "*.pl" -print0 | xargs -0 grep /dev/null

If your find does not support print0, then
  find -name "*.pl" -print | xargs grep /dev/null

The second method will not handle files with new-line in their names.

Also, there is perl. I never use it for trivial tasks as this one,
it seems less elegant :-(

   find2perl ~/bin -name "*"  \
                   -eval 'open(FD,$_); while($txt=<FD>) {
                          $txt=~ /your test/ && print "$_:$txt";}' | perl

  Michael

 
 
 

find *.pl and grep for a string

Post by Rich K » Thu, 19 Mar 1998 04:00:00






>> >I need to find all my files that have  an extention pl  ex *.pl
>> >and find out if these files contains a string date

>> The "grep" command will prepend the file name on any matching lines
>> if you give it more than one file name.

>> What I usually do is add a reference to "/dev/null",
>> so your command simply becomes:

>>     find  . -name "*.pl" -exec grep date {} /dev/null \;

>That's good advice (and often-repeated, I might add).
>But if you only want a list of files that contain the string
>without seeing all the lines it occurs in, you can use "-l"
>option with grep:

>find  . -name "*.pl" -exec grep -l date {} \;

>or somewhat faster

>find  . -name "*.pl" | xargs grep -l date

 In the find example above you forgot to add -print which needed for the
 pipe.

 find . -name "*.pl" -print | xargs grep -l date

 richk

- Show quoted text -

Quote:>--
>Tapani Tarvainen

 
 
 

find *.pl and grep for a string

Post by Tapani Tarvaine » Thu, 19 Mar 1998 04:00:00





> >find  . -name "*.pl" | xargs grep -l date
>  In the find example above you forgot to add -print which needed for the
>  pipe.

>  find . -name "*.pl" -print | xargs grep -l date

Are there still find versions around that actually need the -print?
It must be years since I've seen one... which unix version are
you using?

--
Tapani Tarvainen