Search for Files that DONT contain a string | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Search for Files that DONT contain a string

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-04-2004
tonydsam tonydsam is offline
Registered User
 
Join Date: May 2004
Last Activity: 23 April 2009, 9:39 AM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Data Search for Files that DONT contain a string

How do I search for files that dont contain a certain string? I am currently trying

find ./logs -size +1c -exec grep -l 'Process Complete' {} \; -exec ls -l {} \; >
$TOD

Which gives me files that are reater han 0 file size and contain the string 'Process complete' but I want files that DONT contain Process Complete. If i use the -v option for grep it still doesnt work!
Sponsored Links
    #2  
Old 05-04-2004
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 23 October 2014, 10:56 AM EDT
Location: NM
Posts: 10,250
Thanks: 281
Thanked 803 Times in 747 Posts
Try

Code:
find ./logs -size +1c  > t._tmp
while read filename
do
     grep -q "Process Complete" $filename
     if [ $? -ne 0 ] ; then
             echo $filename
     fi
done < t._tmp
rm -f t_tmp

Sponsored Links
    #3  
Old 05-04-2004
Ygor's Avatar
Ygor Ygor is offline Forum Advisor  
Advisor
 
Join Date: Oct 2003
Last Activity: 2 May 2014, 7:14 AM EDT
Location: 54.23, -4.53
Posts: 1,801
Thanks: 1
Thanked 112 Times in 98 Posts
Re: Search for Files that DONT contain a string

Quote:
Originally posted by tonydsam
How do I search for files that dont contain a certain string? I am currently trying

find ./logs -size +1c -exec grep -l 'Process Complete' {} \; -exec ls -l {} \; >$TOD

Which gives me files that are reater han 0 file size and contain the string 'Process complete' but I want files that DONT contain Process Complete. If i use the -v option for grep it still doesnt work!
Just negate the condition with a "!"

find ./logs -size +1c ! -exec grep -l 'Process Complete' {} \; -exec ls -l {} \; >$TOD
    #4  
Old 05-05-2004
tonydsam tonydsam is offline
Registered User
 
Join Date: May 2004
Last Activity: 23 April 2009, 9:39 AM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
This doesnt seem to be working as it is still picking up the files with Process Complete in them. I have tried moving the ! around but still no joy. 'Process Complete' should only be on the last line of the file if that helps.

Last edited by tonydsam; 05-05-2004 at 03:26 AM..
Sponsored Links
    #5  
Old 05-05-2004
Ygor's Avatar
Ygor Ygor is offline Forum Advisor  
Advisor
 
Join Date: Oct 2003
Last Activity: 2 May 2014, 7:14 AM EDT
Location: 54.23, -4.53
Posts: 1,801
Thanks: 1
Thanked 112 Times in 98 Posts
I think that your problem is that you are using find to grep on the ./logs directory itself and not just on the files in that directory. You need to restrict the find to only grep within plain files.

Lets test this theory, using some test files...

$ mkdir logs
$ echo 'Process Complete' > logs/log1
$ echo 'otherwise' > logs/log2

...first find files that do contain the string...

$ find ./logs -size +1c -exec grep -q 'Process Complete' {} \; -print
./logs/log1

...as expected. Now find files that do NOT contain the string...

$ find ./logs -size +1c ! -exec grep -q 'Process Complete' {} \; -print
./logs
./logs/log2

...woah! What's that "./logs" thing doing there?! If I was using "-exec ls -l {} \;" then it would return every file in the directory! Better restrict the find to plain files...

$ find ./logs -type f -size +1c ! -exec grep -q 'Process Complete' {} \; -print
./logs/log2

...OK!
Sponsored Links
    #6  
Old 05-05-2004
tonydsam tonydsam is offline
Registered User
 
Join Date: May 2004
Last Activity: 23 April 2009, 9:39 AM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you, this does seem to work but I get an error with the -q option so I have changed it to -l, what should it do though ?


Last edited by tonydsam; 05-05-2004 at 10:23 AM..
Sponsored Links
    #7  
Old 05-05-2004
Ygor's Avatar
Ygor Ygor is offline Forum Advisor  
Advisor
 
Join Date: Oct 2003
Last Activity: 2 May 2014, 7:14 AM EDT
Location: 54.23, -4.53
Posts: 1,801
Thanks: 1
Thanked 112 Times in 98 Posts
What kind of grep does not have the -q option?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with search string between two files greet_sed Shell Programming and Scripting 5 03-06-2012 03:25 AM
Search and replace string in files d13g0sv Shell Programming and Scripting 1 03-24-2011 02:42 PM
String Search within Text Files bggibson UNIX for Dummies Questions & Answers 1 09-04-2008 03:44 PM
What is the Best way to search files for a string?? 35Soinc UNIX for Dummies Questions & Answers 15 03-25-2008 04:34 PM
I dont want to know any search engines memattmyself Shell Programming and Scripting 1 05-09-2002 11:09 AM



All times are GMT -4. The time now is 11:07 AM.