Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > Re: How can I c...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 15 of 17 Topic 2207 of 2236
Post > Topic >>

Re: How can I check if a file exists in gawk?

by Ed Morton <morton@[EMAIL PROTECTED] > Mar 30, 2008 at 01:30 PM

On 3/30/2008 10:22 AM, mjc wrote:
> On Mar 29, 2:18 pm, Ed Morton <mor...@[EMAIL PROTECTED]
> wrote:
> 
> ....
> 
>>What contortions? Could you give a small example of the problem?
>>
> 
> .....
> 
>>        Ed.
> 
> 
> I wrote a program in gawk to compare the results of a computation
> (written in assembly language with debugging info) with a simulation
> of the computation. In addition, the program also read the assembly
> listing so it would know what debugging info could be written and read
> its own source so it would know what debugging info was being looked
> for.
> 
> So, there were four files being read - the first three (especially the
> first) needed to be completely read before the last was started.
> 
> I read the first three in the BEGIN block using getline in three
> separate loops. Since there was no overlap, I used the standard
> splitting of $0 (i.e., getline < file). Patterns were matched using
> combinations of "if ( $x == ..." and "if ( match($x, ...)".
> 
> The fourth file was read in the standard pattern-matching from stdin
> paradigm. These patterns were what the program looked for when it read
> itself so it could find out which assembly language debugging
> statements were being looked for.
> 
> At each pattern-match, the assembly-language debug output was compared
> with the corresponding simulation results (read from the first file)
> and statistics about that particular part of the computation gathered
> (min, max, and mean error). If the results were too bad, an error
> message was written and also saved to be written at the end where it
> could be readily noticed.
> 
> At the end, the program compared the assembly listing info with its
> own listing info to tell which debugging statement had not been
> reached (in 3rd and not in 2nd file) and which debugging statements
> had not been looked for (in 2nd and not in 3rd file).
> 
> Finally, the statistics about the final errors were output.
> 
> This was the first time I had a gawk program that read its own source
> - I found that somewhat amusing.
> 
> btw, I always use the "-lint" option, and ignore the "variable
> shadows" and "nonstandard" messages. The other messages I often find
> very helpful.
> 
> I suppose I could check when the record number resets to 1 to see when
> a new file starts and look at FILENAME to see what the file is, but I
> find using getline in this case much more straightforward. In
> particular, I would have to have every pattern check for which file
> the pattern applied to.
> 
> That's my story, a trifle gory, but I don't worry because it's not an
> allegory.
> 
> martin cohen

So, you had something like this:

BEGIN {
	while ((getline < ARGV[1]) > 0) {
		do first file stuff
	}
	close(ARGV[1])
	while ((getline < ARGV[2]) > 0) {
		do second file stuff
	}
	close(ARGV[2])
	while ((getline < ARGV[3]) > 0) {
		do second file stuff
	}
	close(ARGV[3])
	ARGV[1]=ARGV[2]=ARGV[3]=""
}
/pattern/ { pattern match in fourth file }
END { do the end stuff }

when all you really needed was:

ARGIND == 1 { do first file stuff; next }
ARGIND == 2 { do second file stuff; next }
ARGIND == 3 { do third file stuff; next }
/pattern/ { pattern match in fourth file }
END { do the end stuff }

Replace "ARGIND == N" with "FILENAME == ARGV[N]" if you want a solution
that
isn't gawk-specific.

	Ed.




 17 Posts in Topic:
How can I check if a file exists in gawk?
Boltar <boltar2003@[EM  2008-03-27 09:27:48 
Re: How can I check if a file exists in gawk?
Ed Morton <morton@[EMA  2008-03-27 11:36:10 
Re: How can I check if a file exists in gawk?
Boltar <boltar2003@[EM  2008-03-27 09:48:25 
Re: How can I check if a file exists in gawk?
gazelle@[EMAIL PROTECTED]  2008-03-27 16:54:35 
Re: How can I check if a file exists in gawk?
Ed Morton <morton@[EMA  2008-03-27 12:00:41 
Re: How can I check if a file exists in gawk?
Michael Jaritz <ewigli  2008-03-27 23:36:22 
Re: How can I check if a file exists in gawk?
Ed Morton <morton@[EMA  2008-03-27 17:56:28 
Re: How can I check if a file exists in gawk?
stan <smoore@[EMAIL PR  2008-03-27 22:05:43 
Re: How can I check if a file exists in gawk?
Boltar <boltar2003@[EM  2008-03-27 10:04:11 
Re: How can I check if a file exists in gawk?
Ed Morton <morton@[EMA  2008-03-27 13:25:36 
Re: How can I check if a file exists in gawk?
mjc <mjcohen@[EMAIL PR  2008-03-29 13:12:23 
Re: How can I check if a file exists in gawk?
Ed Morton <morton@[EMA  2008-03-29 16:18:38 
Re: How can I check if a file exists in gawk?
mjc <mjcohen@[EMAIL PR  2008-03-30 08:22:43 
Re: How can I check if a file exists in gawk?
gazelle@[EMAIL PROTECTED]  2008-03-30 15:35:38 
Re: How can I check if a file exists in gawk?
Ed Morton <morton@[EMA  2008-03-30 13:30:10 
Re: How can I check if a file exists in gawk?
gazelle@[EMAIL PROTECTED]  2008-03-30 20:30:58 
Re: How can I check if a file exists in gawk?
Janis Papanagnou <Jani  2008-03-30 22:56:14 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri May 16 8:37:08 CDT 2008.