Quantcast
Viewing latest article 2
Browse Latest Browse All 4

Answer by terdon for Grep for This and NOT That in a File?

Here's one that is loosely based on Rich Homolka's answer, but works on directory trees:

find . -type f  -exec grep -l addDesignControlChangeNotification {} \; |  while IFS= read -r file; do    grep -q removeDesignControlChangeNotification "$file"> /dev/null ;    [ $? -ne 0 ] && echo $file;  done

Any files returned will contain addDesignControlChangeNotification but not removeDesignControlChangeNotification.

EXPLANATION:

  • find . -type f -exec grep -l foo {} \;: This will print all files in any subdirectory of the current directory that contain the string foo. The -l flag causes grep to only print the names of matching files.

  • while read file : this iterates through each file found above, saving its name in the variable $file.

  • grep -q bar "$file"> /dev/null : this comand loks for the string bar in each of the files that contained foo.

  • [ $? -ne 0 ] && echo $file; : If the return value ($?) of the grep command was 0 (i.e. if the string was not in the file), print the name of the file.


Viewing latest article 2
Browse Latest Browse All 4

Trending Articles