Using grep to find files that don't contain a string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using grep to find files that don't contain a string
# 1  
Old 04-28-2011
Using grep to find files that don't contain a string

Hi all,

I am still learning my way around unix commands and I have the following question.
I have a website and I want to search for all the html pages that don't contain a certain js file. The file I am searching for is located under /topfolder/js/rules.js . So I assume in my grep search I could search for either "rules.js" or better if possible "/topfolder/js/rules.js".

I want to search topfolder and all files inside that directory and print the results to a file.
Any help I can get is much appreciated.
Thank you
# 2  
Old 04-28-2011
Bug

You should try grep -v


-v, --invert-match
Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.)
# 3  
Old 04-28-2011
This my command to match the string:
find ./ -name '*.html' -exec grep -l "/top/js/file1.js" {} \; 2>/dev/null 1>./output.txt &

The command above prints all the html files that have a reference to the js file.

Unfortunately, I am unable to use -v or -L. -vl prints all the files with or without the js reference. -L only doesn't print anything.

Could you please tell me what I am doing wrong? Tx
# 4  
Old 04-28-2011
Code:
find ./ -name '*.html' |
while read filename
  grep -q '/top/js/file1.js' $filename || echo $filename > output.txt
done

Try that. Be sure your grep supports -q first.
# 5  
Old 04-28-2011
So I put the above in a script and when I executed it I got an error.

$ ksh rule1
rule1: ^M: not found
rule1[2]: syntax error at line 4 : `done' unexpected

Please know that I am still learning about Unix/Linux.
Also how do I check if my ksh supports grep -q ?
# 6  
Old 05-09-2011
gnu grep & gnu find:
find ./ -name '*.html' -exec grep -L "/top/js/file1.js" {} \; 2>/dev/null 1>./output.txt

-L is without match, -l is with match. also, the -v is not useful here, as grep with the -L effectively searches the whole file, and the -v is for the usual single line search (programmatically, I'm sure it's differently, but effectively is what counts here)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep to find an exact string

Hi all, I tried searching the forum for this, and I read numerous suggestions here and even on other forums, and I cannot get this to want the way that I need it to. I tried grep -W / -f to no luck. Here is what I have. I have a list of file names- FILE1-FILE1TEST,FILE1RELATION... (7 Replies)
Discussion started by: jeffs42885
7 Replies

2. Shell Programming and Scripting

Sleep till grep find the string

Hello! I have a sample code in which a grep is being performed from multiple files with a date format in their naming convention. Here the script: #! /usr/bin/bash cd /IN/service_packages/SMS/cdr/received MYDATE=`date +"%Y%m%d%H%M"` #get the value then divide by 60 #DAPS_SLC01... (3 Replies)
Discussion started by: nms
3 Replies

3. Shell Programming and Scripting

Grep string in files and list file names that contain the string

Hi, I have a list of zipped files. I want to grep for a string in all files and get a list of file names that contain the string. But without unzipping them before that, more like using something like gzcat. My OS is: SunOS test 5.10 Generic_142900-13 sun4u sparc SUNW,SPARC-Enterprise (8 Replies)
Discussion started by: apenkov
8 Replies

4. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

5. UNIX for Dummies Questions & Answers

Find files that don't match expression

How Do I find all files in /home that are not .jpgs? (5 Replies)
Discussion started by: glev2005
5 Replies

6. Shell Programming and Scripting

Deleting files that don't contain particular text strings / more than one instance of a string

Hi all, I have a directory containing many subdirectories each named like KOG#### where # represents any digit 0-9. There are several files in each KOG#### folder but the one I care about is named like KOG####_final.fasta. I am trying to write a script to copy all of the KOG####_final.fasta... (3 Replies)
Discussion started by: kmkocot
3 Replies

7. UNIX for Dummies Questions & Answers

grep string and then find another string

I have a file that looks like this: Name=TOM abcded asdfas fkoiaerj inadhah Name=Chris 23nafds vasdkfna afadns afdadsfa aaaaaa bbbbbb cccccc I would to search for the string 'bbbbbb', then I would like to search above that string for the 1st occurrence of "Name" and not the other... (7 Replies)
Discussion started by: doza22
7 Replies

8. Shell Programming and Scripting

find exarct string using grep

Hi i have t1 file like: HiPerformance: Recalculation Dates|234| HiPerformance: Recalculation|456| ..... ..... .... ... i wrote grep command like: grep "HiPerformance: Recalculation" t1 out put: HiPerformance: Recalculation Dates|234| HiPerformance: Recalculation|456| But i... (3 Replies)
Discussion started by: koti_rama
3 Replies

9. Shell Programming and Scripting

grep string and find line before

hi, i have to grep for string in file but i want to find the group of this line so i must get lines before and select the group. the file look like : ####name_groupe1 alphanumeric line alphanumeric line .. ####name_groupe2 alphanumeric line alphanumeric line .. ####name_groupe3... (4 Replies)
Discussion started by: kamel.seg
4 Replies

10. AIX

Help Using Grep command to Find the string in the files in the root directory

I want to serch for a string in all the files in the root directory. i want the search to be limited to only the files in the directory i.e the search to be done only in the files not in the sub directory. the approaches tried are 1)grep "pattern string" this command was serching the... (3 Replies)
Discussion started by: Subbu_Angeline
3 Replies
Login or Register to Ask a Question