Text File Manipulation

Hi,

I need to write shell script for the scenario explained below -

datafile.txt

AcctNum,code,Region,,,,
12345451,AN ,abaab
12345452,AN ,xccxc
76677545,RP ,acxcc
43567878,RP ,afghh
32190900,AN ,afrfrf
87312345,AN ,aqaw

I have a text file (datafile.txt) which has data with above format.
This file has many more columns and can have huge number of the records.

I have to process the above file and need to send the records into another text file (a1.txt) but this file can have maximum 10k records for a particular 'code' say 'AN' and other records should be moved to another file a2.txt. i.e if there are records more than 10k for a particular 'code' then the records number 10001 onwards will be moved to file a2.txt

Thanks in advance.

For code "AN" it should be like this:

awk -F, '$2==code{ if(++n%10000) {c++}; print > "a" c ".txt" }' code="AN " datafile.txt