Aller au contenu | Aller au menu | Aller à la recherche


grep contain and not contain

How to find all files containing "yyy" and also not containing "zzz" for each files matching "yyy" ? Resolved with 2 grep.

grep regex containing 'yyy' but not containing 'zzz' :

  1. First, finding all files containing 'yyy', and keep in one file
grep -r -l "yyy" /c/folder/ > grepsearch.txt
  1. Second, call this script : grepsearch.sh who is looping on first result file and search number of occurencies of the second search 'zzz'
for file in `cat grepsearch.txt`; do 
   # echo "---------------------------------------------------------- $file"; 
   grepcount=$(grep -c 'zzz' $file); 
#   echo $grepcount $file; 
   if [ $grepcount -eq 0 ]
   then
#      echo "No matching search zzz";
      echo $file;
#   else
#      echo "Match search zzz";
   fi
done

Note : if your search contain "\" add a second : "\\"

Ajouter un commentaire

Le code HTML est affiché comme du texte et les adresses web sont automatiquement transformées.

Fil des commentaires de ce billet