AWK question

dce at mips.UUCP dce at mips.UUCP
Wed Jan 28 01:11:19 AEST 1987


In article <3746 at brl-adm.ARPA> lesh at BRL.ARPA (ISC | howard) writes:
>	THE PROBLEM:
>	"Users should also note that there is an upper limit to the
>number of files that are written in this way.  At present it is ten."*1
>
>	I can't find any way to close a file opened by 'awk' and very
>soon get the "too many opened files error message". 
>
>	Any suggestions?

Don't try to close the file (even though some newer versions of awk
may have a close builtin). I have used the following methods quite
successfully:

1. Iteration - Keep track of the number of files you have used. At that
   point, put all data that goes to a new file onto the standard output.
   By redirecting standard output to a temporary file, you can check after
   the awk script has finished to see if this file is empty. If not, run
   the script again with the temporary file as the input. Otherwise, you
   are finished.

   This method doesn't work when there is a lot of state involved.

2. Pass-thru - Instead of writing the records directly to the file, write
   records of the form

	filename data...

   and use the construct:

		awk ... | while read file data
		do
			echo "$data" >> "$file"
		done

   (functions can make this look a lot cleaner).

   The only problem with this is that the shell read command eats backslashes
   (bug?). If this is unacceptable, you could get away with generating

	filename
	data...

   and use calls to the line command (head -1 may not work correctly here)
   to get the data.
-- 
			David Elliott

UUCP: 	{decvax,ucbvax,ihnp4}!decwrl!mips!dce, DDD:  	408-720-1700



More information about the Comp.unix.questions mailing list