Extract data from a file

I have a text file that contains the following data. For example, aa.txt has some numbers. I need to extract the continuous numbers(minimum 3 numbers) from it.How can I do this with awk?

>aa.txt
31
35
36
37
38
39    
44
169
170
173
174
175
177
206 
>1a.txt
39
40
41
42
146
149
151

Desired output

>aa_1.txt
35
36 
37
38
39
>aa_2.txt
173
174
175
>1a.txt
39
40
41
42

your example for aa_1.txt output shows more than 3 numbers.
You should be able to do something like

sort -n filename.txt | head -3

Note:- 1a.txt shows 39 to 42 inclusive as consecutive.

This is 4 numbers, not 3 as you show.

Do you only want numbers beginning with 4x from 1a.txt?
If yes then this is contradiction in terms...

Please be more specific...

EDIT:
Also you have a space in the 2nd and 3rd series is this also a requirement?

You are right! I have corrected it. Space is not a requirement.

An awk approach:

awk '
        BEGIN {
                c = 1
        }
        {
                A[NR] = $1
        }
        END {
                for ( i = 1; i < NR; i++ )
                {
                        d = A[i+1] - A

                        F = FILENAME
                        sub ( /\.txt/, "_" c ".txt", F )

                        if ( d == 1 )
                        {
                                printf "%d\n", A > F
                                f = 1
                        }

                        if ( f && d != 1 )
                        {
                                printf "%d\n", A > F
                                f = 0
                                c += 1
                        }
                }
        }
' aa.txt
1 Like
arr=[]
with open("a.txt") as file:
 for line in file:
  line=line.replace("\n","")
  if not arr:
    arr.append(line)
  elif int(arr[-1])+1==int(line):
    arr.append(line)
  else:
    if len(arr)>=3:
        print("\n".join(arr))
        print("")
    arr=[line]        
    
if len(arr)>=3:
    print("\n".join(arr))