Extract multiple repeated data from a text file

Hi,

I need to extract data from a text file in which data has a pattern. I need to extract all repeated pattern and then save it to different files.
example:

input is:

ST*867*000352214
BPT*00*1000352214*090311
SE*1*1

ST*867*000352215
BPT*00*1000352214*090311
SE*1*2

ST*867*000352216
BPT*00*1000352214*090311
SE*1*3

output is like:

first file:

ST*867*000352214
BPT*00*1000352214*090311
SE*1*1

second file:

ST*867*000352215
BPT*00*1000352214*090311
SE*1*2

Third File:

ST*867*000352216
BPT*00*1000352214*090311
SE*1*3

You see , output is splitted into three different files based on ST-SE segment repeated in the file.

Can anybody please help me to put me close to solution?

Thanks in advance.

Regards,
Neeraj

Try this:

awk '/^ST/{++i}{print > "file" i}' textfile

Regards

seems perl is a good choice to handle it.

$/="\n\n";
open $fh,"<","a.txt";
while(<$fh>){
	my $tmp=sprintf("%s.txt",$.);
	open my $fh1,">",$tmp;
	print $fh1 $_;
	close $fh1;
}

Please explain your code

Thanks
white

Thanks all of you.I used the above code and it worked perfectly.

Thanks once again.

Regards
Neeraj

awk '/^ST/{++i}{print > "file" i}' textfile
/^ST/{++i}

If the line begins with ST increase the variable i. The first time the name of the output file is file1, the next time the line begins with ST the name of the output file becomes file2 and so forth.

{print > "file" i}

Print the line to the file "file"i.

Regards