From 42fc4d3fd60ae9e1791c52a507c220eec8f39fb9 Mon Sep 17 00:00:00 2001 From: Chris Koeritz Date: Wed, 10 Dec 2014 16:24:22 -0500 Subject: [PATCH] simple approach to finding files that contain two patterns; it's easy to find a file that has one pattern or the other, but grep doesn't offer an option saying "both of these must be present". --- scripts/text/grep_two_patterns.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 scripts/text/grep_two_patterns.sh diff --git a/scripts/text/grep_two_patterns.sh b/scripts/text/grep_two_patterns.sh new file mode 100644 index 00000000..6ba40978 --- /dev/null +++ b/scripts/text/grep_two_patterns.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +folder="$1"; shift +pat1="$1"; shift +pat2="$1"; shift + +if [ -z "$pat1" -o -z "$pat2" ]; then + echo "this script requires a folder and two patterns to search." + echo "all files in the folder (and sub-folders) that contain both patterns" + echo "will be displayed." + exit 1 +fi + +find "$folder" -type f -exec grep -lZ "$pat1" ';' | xargs -0 grep -l "$pat2" + -- 2.34.1