splitting tab-delimited file with awk

Hi all, I need help to split a tab-delimited list into separate files by the filename-field. The list is already sorted ascendingly by filename, an example list would look like this;

filename001 word1 word2
filename001 word3 word4
filename002 word1 word2
filename002 word3 word4
filename002 word5 word6

I have tried this using a slightly modified bash script which I found here on unix.com :

awk -F"\t" 'OFS="\t"{print >> ($1 ".tab")}' tabdelimitedfile.tab

This outputs the files filename001.tab and filename002.tab

Unfortuneatly this will not work on larger files than the example listed above if too many different file names are found. I get the error message "awk: *.tab makes too many open files" on exit.

I would be grateful if anyone could suggest an alternative way which outputs one file at a time every time a new filename is encountered.

Cheers

Per

tab="         "        # press the tab key add a quote at the end

OLD_IFS="$IFS"
rm -f filename 0*  # remove past runs of output files
while IFS="$tab" && read f1 f2 f3
do      
      echo "$f1$tab$f2$tab$f2" >> "$f1"
done < inputfile
IFS="$OLD_IFS"

This may not perform all that well with the number of file open and dup calls when you have a huge input file

Maybe this is what you are looking for:

while read newfile text;do echo "$text" >> ${newfile}.tab;done < file

With awk:

(use nawk or /usr/xpg4/bin/awk on Solaris):

awk -F'\t' '!_[$1]++ { 
  fn && close(fn)
  fn = $1 ".tab"
  }
{ print > fn }
' tabdelimitedfile.tab  

Thanks, that last solution by radoulov was exactly what I was looking for. Thank you very much for your solutions!