You can replace the echo "$file" command with any other command you want to perform on the matching "$file" e.g. to cat the contents
Code:
while read -rd $'\0' file; do grep -qIw 'pattern1' "$file" && grep -qIw 'pattern2' "$file" && cat "$file"; done < <(find . -type f -print0)
or to echo the name and then cat the contents - note the use of a subshell ( ... )
Code:
while read -rd $'\0' file; do grep -qIw 'pattern1' "$file" && grep -qIw 'pattern2' "$file" && (echo "$file: "; cat "$file"); done < <(find . -type f -print0)
If your pattern1 and pattern2 are variables you will need to use double quotes not single quotes, like "$pattern1", "$pattern2" for example
Code:
read -p "Please enter the first pattern: " pattern1
read -p "Please enter the second pattern: " pattern2
while read -rd $'\0' file; do
grep -qIw "$pattern1" "$file" && grep -qIw "$pattern2" "$file" && (echo "$file: "; cat "$file")
done < <(find . -type f -print0)
Bookmarks