If you have a lot of very large files, regex matching in awk will be slow. One approach is to leverage grep to do the hard lifting, and awk to build the list of strings to search for (since you don't want to hard code those). I.e.
$grep -E '12|98|23|87|34|76|45|65|56|54|67|43|78|32|89|21' data.log
Does the trick for two characters, but we want to be able to do this for up to 9 characters. You need -E for extended grep to support searching multiple patterns (12|98 are two patterns) - plain ol' grep doesn't let you do that.
Awk can loop over the string 123456789 pulling out successive pieces, but we want to go forward and backward, so:
$awk 'BEGIN {f=123456789 ; b=987654321 ; for(i=1;i<9;i++) print substr(f,i,2),substr(b,i,2)}'
12 98
23 87
34 76
45 65
56 54
67 43
78 32
89 21
Let's add something so the length isn't hard coded to two (-vn=3 sets the variable n=3 inside the awk script):
$awk -vn=3 'BEGIN {f=123456789 ; b=987654321 ; for(i=1;i<11-n;i++) print substr(f,i,n),substr(b,i,n)}'
123 987
234 876
345 765
456 654
567 543
678 432
789 321
And (almost there!) get the pipe symbol grep -E wants by changing output record separator (ORS) and output field separator (OFS) to |
$awk -vn=3 'BEGIN {ORS="|" ; OFS="|" ; f=123456789 ; b=987654321 ; for(i=1;i<11-n;i++) print substr(f,i,n),substr(b,i,n)}'
123|987|234|876|345|765|456|654|567|543|678|432|789|321|
We have to get rid of the final pipe after 321, or grep will match everything, so add sed '.$//' to replace the last character before the end of the string ($) with nothing:
$awk -vn=3 'BEGIN {ORS="|" ; OFS="|" ; f=123456789 ; b=987654321 ; for(i=1;i<11-n;i++) print substr(f,i,n),substr(b,i,n)}' | sed 's/.$//'
And now we can put it together in a shell script that lets us do the search in general:
$cat t.sh
#!/bin/bash
grep -E `awk --assign n=$1 'BEGIN {OFS="|" ; ORS="|" ; f=123456789 ; b=987654321 ; for(i=1;i<11-n;i++) print substr(f,i,n),substr(b,i,n)}' | sed 's/.$//'` $2
$chmod 775 t.sh
$./t.sh 4 data.log
1234
7654
1234567
1234567.