Check for Pattern if exists write to file

Hi ! All

I just want to search and write to new file if pattern is found in text file

following are my text files by which I want to search Month and last column number

my text file1

15-Jan-2011 25 ARTS 1255 125 125 178 198 
15-Jan-2011 25 ARTS 1255 125 125 178 198 
15-Jan-2011 25 ARTS 1255 125 125 178 198 
15-Jan-2011 25 ARTS 1255 125 125 178 198 

my text file2

15-Feb-2011 25 ARTS 1255 125 125 178 119 
15-Feb-2011 25 ARTS 1255 125 125 178 119 
15-Feb-2011 25 ARTS 1255 125 125 178 119 
15-Feb-2011 25 ARTS 1255 125 125 178 119 

my text file3

15 1 2011 25 ARTS 1255 125 125 178 112
15 1 2011 25 ARTS 1255 125 125 178 112
15 1 2011 25 ARTS 1255 125 125 178 112
15 1 2011 25 ARTS 1255 125 125 178 112

my text file4

15 2 2011 25 ARTS 1255 125 125 178 158
15 2 2011 25 ARTS 1255 125 125 178 158
15 2 2011 25 ARTS 1255 125 125 178 158
15 2 2011 25 ARTS 1255 125 125 178 158

ultimately I want to create new files like this

Jan_198_1.txt
Jan_112_2.txt
Feb_119_1.txt
Feb_158_2.txt

Any help in this regard is appreciated.

What have you tried?

Do you want to create empty files based on your pattern?

No I want to copy contents of $file to new file to be created.

---------- Post updated at 09:13 AM ---------- Previous update was at 05:26 AM ----------

please tel me.. whether its possible or impossible in bash ?

How about an awk program?

awk -F'[- ]' ' BEGIN {
        mon = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
        split ( mon, M );
} {
        sub (/[ \t]+$/, X)
        for ( i = 1; i <= 12; i++ )
        {
                if ( $2 == M || $2 == i )
                {
                        n = M "_" $NF
                        if ( n in FA )
                        {
                                F = M "_" $NF "_" C ".txt"
                        }
                        else
                        {
                                C += 1
                                F = M "_" $NF "_" C ".txt"
                                n = M "_" $NF
                                FA[n] = n
                        }
                        print $0 >> F
                }
        }
        close (F)
} ' file*

Use nawk instead in SunOS or Solaris