Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!rutgers!mit-eddie!genrad!decvax!decwrl!pyramid!prls!mips!dce From: dce@mips.UUCP Newsgroups: comp.unix.questions Subject: Re: AWK question Message-ID: <154@quacky.mips.UUCP> Date: Tue, 27-Jan-87 10:11:19 EST Article-I.D.: quacky.154 Posted: Tue Jan 27 10:11:19 1987 Date-Received: Sat, 7-Feb-87 11:04:05 EST References: <3746@brl-adm.ARPA> Reply-To: dce@quacky.UUCP (David Elliott) Organization: MIPS Computer Systems, Sunnyvale, CA Lines: 49 In article <3746@brl-adm.ARPA> lesh@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