grep contain and not contain
Par PlaceOweb le dimanche, juin 14 2020, 17:34 - Système - Lien permanent
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' :
- First, finding all files containing 'yyy', and keep in one file
grep -r -l "yyy" /c/folder/ > grepsearch.txt
- 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 : "\\"