Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.unix.programmer > #890 > unrolled thread

Find / Grep Question

Started byMartin Crouch <spamspamspam@spaml.com>
First post2011-07-07 16:26 +0100
Last post2011-07-10 15:35 +0100
Articles 12 — 7 participants

Back to article view | Back to comp.unix.programmer


Contents

  Find / Grep Question Martin Crouch <spamspamspam@spaml.com> - 2011-07-07 16:26 +0100
    Re: Find / Grep Question Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-07-07 16:37 +0100
      Re: Find / Grep Question Martin Crouch <spamspamspam@spaml.com> - 2011-07-07 16:47 +0100
        Re: Find / Grep Question Mc <spam.mc@gmail.com> - 2011-07-07 16:49 +0100
        Re: Find / Grep Question Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-07-07 17:22 +0100
        Re: Find / Grep Question John Gordon <gordon@panix.com> - 2011-07-07 16:28 +0000
    Re: Find / Grep Question Bjarni Juliusson <bjarni@update.uu.se> - 2011-07-07 19:06 +0200
      Re: Find / Grep Question wfw <wyatt@cfa.harvard.edu> - 2011-07-07 21:59 -0700
        Re: Find / Grep Question wfw <wyatt@cfa.harvard.edu> - 2011-07-07 22:01 -0700
      Re: Find / Grep Question wfw <wyatt@cfa.harvard.edu> - 2011-07-07 22:14 -0700
        Re: Find / Grep Question Martin Crouch <spamspamspam@spaml.com> - 2011-07-08 10:04 +0100
    Re: Find / Grep Question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-07-10 15:35 +0100

#890 — Find / Grep Question

FromMartin Crouch <spamspamspam@spaml.com>
Date2011-07-07 16:26 +0100
SubjectFind / Grep Question
Message-ID<4e15d020$0$29268$c3e8da3$10cdda79@news.astraweb.com>
Here's a quick question:

I can find all files containing the string "dir" in a directory (and 
subdirectories) by doing the following:

find . | xargs grep -ils dir

However how can I do the reverse?  i.e. Find all the files that do not 
contain the string "dir".   Sounds simple, but I haven't found an easy 
way yet.

Any pointers welcome.

Cheers.

[toc] | [next] | [standalone]


#891

FromRainer Weikusat <rweikusat@mssgmbh.com>
Date2011-07-07 16:37 +0100
Message-ID<87oc16cda0.fsf@sapphire.mobileactivedefense.com>
In reply to#890
Martin Crouch <spamspamspam@spaml.com> writes:
> I can find all files containing the string "dir" in a directory (and
> subdirectories) by doing the following:
>
> find . | xargs grep -ils dir
>
> However how can I do the reverse?  i.e. Find all the files that do not
> contain the string "dir".   Sounds simple, but I haven't found an easy
> way yet.

find . | xargs grep -vils dir

[toc] | [prev] | [next] | [standalone]


#892

FromMartin Crouch <spamspamspam@spaml.com>
Date2011-07-07 16:47 +0100
Message-ID<4e15d513$0$4528$c3e8da3$460562f1@news.astraweb.com>
In reply to#891
On 07/07/2011 16:37, Rainer Weikusat wrote:
> Martin Crouch<spamspamspam@spaml.com>  writes:
>> I can find all files containing the string "dir" in a directory (and
>> subdirectories) by doing the following:
>>
>> find . | xargs grep -ils dir
>>
>> However how can I do the reverse?  i.e. Find all the files that do not
>> contain the string "dir".   Sounds simple, but I haven't found an easy
>> way yet.
>
> find . | xargs grep -vils dir
Thanks.

Unfortunately that doesn't seem to work as the files have other content 
in (they are more than one line)  so I'm assuming those lines get listed 
by the -v option and so the file gets shown even though the file does 
contain "dir".   Here's what I've just worked out so far:

find . | sort >/tmp/l1
find . | xargs grep -ils dir >/tmp/l2
comm -23 /tmp/l1 /tmp/l2

i.e Create a temp file containing all the filenames,  then another one 
containing the ones that have my text.  Then find the lines that are 
unique to /tmp/l1.   I think that's working, but it's hardly elegant.

[toc] | [prev] | [next] | [standalone]


#893

FromMc <spam.mc@gmail.com>
Date2011-07-07 16:49 +0100
Message-ID<iv4kia$g6$1@dont-email.me>
In reply to#892
  Here's what I've just worked out so far:
>
> find . | sort >/tmp/l1
> find . | xargs grep -ils dir >/tmp/l2
> comm -23 /tmp/l1 /tmp/l2
>
> i.e Create a temp file containing all the filenames, then another one
> containing the ones that have my text. Then find the lines that are
> unique to /tmp/l1. I think that's working, but it's hardly elegant.
>

Oops...

/tmp/l2 should be sorted too for comm to function correctly.

find . | xargs grep -ils dir | sort >/tmp/l2

[toc] | [prev] | [next] | [standalone]


#894

FromRainer Weikusat <rweikusat@mssgmbh.com>
Date2011-07-07 17:22 +0100
Message-ID<87k4bucb7v.fsf@sapphire.mobileactivedefense.com>
In reply to#892
Martin Crouch <spamspamspam@spaml.com> writes:
> On 07/07/2011 16:37, Rainer Weikusat wrote:
>> Martin Crouch<spamspamspam@spaml.com>  writes:
>>> I can find all files containing the string "dir" in a directory (and
>>> subdirectories) by doing the following:
>>>
>>> find . | xargs grep -ils dir
>>>
>>> However how can I do the reverse?  i.e. Find all the files that do not
>>> contain the string "dir".   Sounds simple, but I haven't found an easy
>>> way yet.
>>
>> find . | xargs grep -vils dir
> Thanks.
>
> Unfortunately that doesn't seem to work as the files have other
> content in (they are more than one line)

Sorry, I misread that. If you have GNU grep, you could use -L instead
of -l:

[root@quicksand]/etc #grep --help | grep -- -L
  -L, --files-without-match print only names of FILEs containing no match

[toc] | [prev] | [next] | [standalone]


#895

FromJohn Gordon <gordon@panix.com>
Date2011-07-07 16:28 +0000
Message-ID<iv4mrn$73c$1@reader1.panix.com>
In reply to#892
In <4e15d513$0$4528$c3e8da3$460562f1@news.astraweb.com> Martin Crouch <spamspamspam@spaml.com> writes:

> Unfortunately that doesn't seem to work as the files have other content 
> in (they are more than one line)  so I'm assuming those lines get listed 
> by the -v option and so the file gets shown even though the file does 
> contain "dir".

Grep only cares about *lines* of text.  (Even after using unix for many
years, I have to remind myself of this fact every so often.)

The -l option is not shorthand for "evaluate the contents of the file
as a whole"; it is shorthand for "print only the filename instead of all
the matching lines."

As a result the -v and -l options tend not to work well together, because
most files will have some lines that don't contain the target text, and so
grep happily reports them.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#896

FromBjarni Juliusson <bjarni@update.uu.se>
Date2011-07-07 19:06 +0200
Message-ID<iv4p32$5rb$1@Iltempo.Update.UU.SE>
In reply to#890
On 07/07/2011 05:26 PM, Martin Crouch wrote:
> Here's a quick question:
>
> I can find all files containing the string "dir" in a directory (and
> subdirectories) by doing the following:
>
> find . | xargs grep -ils dir
>
> However how can I do the reverse? i.e. Find all the files that do not
> contain the string "dir". Sounds simple, but I haven't found an easy way
> yet.

How about

   find . \! -exec grep -qi dir '{}' \; -print

?


Bjarni
-- 

                        INFORMATION WANTS TO BE FREE

[toc] | [prev] | [next] | [standalone]


#910

Fromwfw <wyatt@cfa.harvard.edu>
Date2011-07-07 21:59 -0700
Message-ID<dc246f88-7830-4a11-b909-1e665895a159@34g2000yqr.googlegroups.com>
In reply to#896
On Jul 7, 1:06 pm, Bjarni Juliusson <bja...@update.uu.se> wrote:
> On 07/07/2011 05:26 PM, Martin Crouch wrote:
>
> > Here's a quick question:
>
> > I can find all files containing the string "dir" in a directory (and
> > subdirectories) by doing the following:
>
> > find . | xargs grep -ils dir
>
> > However how can I do the reverse? i.e. Find all the files that do not
> > contain the string "dir". Sounds simple, but I haven't found an easy way
> > yet.
>
You are making it harder than need be. Try:
   find . \! -name \*dir\* -print

for files without "dir" (add  -type f  iff only ordinary files should
be tested), and
   find . -name \*dir\* -print

for the orginal problem.

[toc] | [prev] | [next] | [standalone]


#911

Fromwfw <wyatt@cfa.harvard.edu>
Date2011-07-07 22:01 -0700
Message-ID<a106d2c1-c813-4946-b72b-655d32ed0c2a@x12g2000yql.googlegroups.com>
In reply to#910
On Jul 8, 12:59 am, wfw <wy...@cfa.harvard.edu> wrote:
> On Jul 7, 1:06 pm, Bjarni Juliusson <bja...@update.uu.se> wrote:> On 07/07/2011 05:26 PM, Martin Crouch wrote:
>
> > > Here's a quick question:
>
> > > I can find all files containing the string "dir" in a directory (and
> > > subdirectories) by doing the following:
>
> > > find . | xargs grep -ils dir
>
> > > However how can I do the reverse? i.e. Find all the files that do not
> > > contain the string "dir". Sounds simple, but I haven't found an easy way
> > > yet.
>
> You are making it harder than need be. Try:
>    find . \! -name \*dir\* -print
>
> for files without "dir" (add  -type f  iff only ordinary files should
> be tested), and
>    find . -name \*dir\* -print
>
> for the orginal problem.

Darn - I missed 'contain the string'. Posted waay too fast.

[toc] | [prev] | [next] | [standalone]


#912

Fromwfw <wyatt@cfa.harvard.edu>
Date2011-07-07 22:14 -0700
Message-ID<e49b09ed-941e-4bb9-940a-3947aba8a7bb@o13g2000yqj.googlegroups.com>
In reply to#896
On Jul 7, 1:06 pm, Bjarni Juliusson <bja...@update.uu.se> wrote:
> On 07/07/2011 05:26 PM, Martin Crouch wrote:
>
> > Here's a quick question:
>
> > I can find all files containing the string "dir" in a directory (and
> > subdirectories) by doing the following:
>
> > find . | xargs grep -ils dir
>
> > However how can I do the reverse? i.e. Find all the files that do not
> > contain the string "dir". Sounds simple, but I haven't found an easy way
> > yet.

Try awk'ing for 'dir' and print only the filenames where it never
occurs:

find . -type f -print| xargs -L 1 awk '/dir/ {n+=1} END {if(n==0)
print FILENAME}'

[toc] | [prev] | [next] | [standalone]


#913

FromMartin Crouch <spamspamspam@spaml.com>
Date2011-07-08 10:04 +0100
Message-ID<4e16c80a$0$1704$c3e8da3$76491128@news.astraweb.com>
In reply to#912
> Try awk'ing for 'dir' and print only the filenames where it never
> occurs:
>
> find . -type f -print| xargs -L 1 awk '/dir/ {n+=1} END {if(n==0)
> print FILENAME}'
>
>

That works great,  I've go a new shell alias now.

Thanks very much.

[toc] | [prev] | [next] | [standalone]


#925

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2011-07-10 15:35 +0100
Message-ID<0.f22cca6dc80a3ad58316.20110710153536BST.87vcva2og7.fsf@bsb.me.uk>
In reply to#890
Martin Crouch <spamspamspam@spaml.com> writes:

> Here's a quick question:
>
> I can find all files containing the string "dir" in a directory (and
> subdirectories) by doing the following:
>
> find . | xargs grep -ils dir
>
> However how can I do the reverse?  i.e. Find all the files that do not
> contain the string "dir".   Sounds simple, but I haven't found an easy
> way yet.
>
> Any pointers welcome.

You have more than one solution already, but I'd thought of this one
before I'd read the others (most of which are better but I hate to
waste a thought...)

  ... | xargs grep -ic dir | sed -ne 's/:0$//p'

This pointlessly counts matches after the first.  If you have the -m
option then

  ... | xargs grep -icm 1 dir | sed -ne 's/:0$//p'

is better but then if you have -m you probably have -L as well.

-- 
Ben.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.unix.programmer


csiph-web