Your post doesn't mention any requirement for regular expression support, so I'm going to assume that you will be searching for fixed, literal text strings.
This probably isn't the fastest algorithm you've ever seen, but it works, if you have enough time. It has the slight defect that if there are more than one N-line patterns that begin with the same first line and have the same SHA256 hash, it will give incorrect results. It assumes that all possible N-line patterns will have unique SHA256 hashes.
It will be tediously slow on large files, especially those which contain numerous occurrences of the first line of the pattern.
# What's the name of the list file?
# What's the name of the pattern file?
# We'll figure out how many times the pattern lines appear (consecutively) in the list.
# Where's your SHA256 tool?
# what's the first line of pattern?
PATTERN_START="$(head -1 $PATTERN)"
# where in the list does that single line appear (what line numbers?)
START_LINES="$(grep -nx "$PATTERN_START" $LIST | sed -e 's/:.*//')"
# how many lines long is the pattern?
PAT_LEN="$(grep -c ^ < $PATTERN)"
echo Pattern is $PAT_LEN lines long, and might start at any of these lines:
PAT_HASH="$($SHA256 < "$PATTERN")"
# So how many times does $PATTERN appear consecutively in $LIST?
for LINE in $START_LINES
HASH="$(tail +$LINE $LIST | head -$PAT_LEN | $SHA256 -q)"
if [ "$HASH" = "$PAT_HASH" ]
echo match at line $LINE
echo The pattern was found $PAT_COUNT times
$ cat list
$ cat pattern
$ . foo.sh
Pattern is 3 lines long, and might start at any of these lines:
2 6 9 11
match at line 2
match at line 6
match at line 11
The pattern was found 3 times