Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion More on BEGINFILE / ENDFILE
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Aharon Robbins  
View profile  
 More options Jan 22 2009, 12:28 am
Newsgroups: comp.lang.awk
From: arn...@skeeve.com (Aharon Robbins)
Date: Thu, 22 Jan 2009 05:28:47 +0000 (UTC)
Local: Thurs, Jan 22 2009 12:28 am
Subject: Re: More on BEGINFILE / ENDFILE
In article <gl6qdb$kb...@heraldo.rediris.es>,
Manuel Collado  <m.coll...@invalid.domain> wrote:

>> Um, I don't know what you're referring to here:

>> $ gawk-3.1.6 'BEGIN { print (getline x < "/no/file") }'
>> -1

>> Am I missing someting?

>I was speaking about non-redirected getline:

>$ gawk 'BEGIN { print getline x }' /no/file
>gawk: fatal: cannot open file `/no/file' for reading (No such file or directory)

Ah. Well, this goes back to the fact that command-line filenames are fatal
errors when they can't be opened.  It is historical practice and consitent
with the other awks:

$ mawk 'BEGIN { print getline x; print 1 }' /no/file
mawk: cannot open /no/file (No such file or directory)

$ nawk 'BEGIN { print getline x; print 1 }' /no/file
nawk: can't open file /no/file
 source line number 1

I understand your point, but I worry that it is too big a break with
historical compatibility.

>>> Please also consider that ENDFILE is an appropriate place to catch
>>> errors while reading records.

>> What kind of errors show up while reading records that are catchable?
>> Can you give me an explicit example, because I'm not understanding you.

>I wonder if the following code could be used to test for readability of
>a set of files, and report failures:

>BEGINFILE {
>   if (ERRNO) {
>     print "error opening " FILENAME ": " ERRNO
>     ERRNO = ""
>   }
>}

That works nows; you should then add a nextfile to skip the bad file.

>ENDFILE {
>   if (ERRNO) {
>     print "error reading " FILENAME " after line " FNR ": " ERRNO
>   }
>}

I don't think anything would ever get here;  If the file cannot be opened
or is a directory, you catch it in the BEGINFILE block.

If the file was already successfully opened, and if, say, an NFS file
went away and read() returned -1, then getline would return -1, instead
of falling into the ENDFILE block.

>Same by using getline alone:

>BEGIN {
>   while ((getline x) != 0) {
>     if ((getline x) < 0) {
>       print "error reading " FILENAME " after line " FNR ": " ERRNO
>     }
>   }
>}

(Your loop, BTW, checks the file's readability for every record read;
not very efficient.)

The gawk manual shows you how to do this by simply looping through ARGV,
using a redirected getline to test for readability and then removing
the bad element from ARGV.  Changing gawk so that the above would work
would break historical compatibility for no really good reason.

>If input errors are not flagged as fatal, the above examples will
>process all arguments, even some of them are unreadable.

I understand, but I think the current BEGINFILE semantics give you
the hooks to handle things adequately.

Thanks,

Arnold
--
Aharon (Arnold) Robbins                                 arnold AT skeeve DOT com
P.O. Box 354            Home Phone: +972  8 979-0381
Nof Ayalon              Cell Phone: +972 50  729-7545
D.N. Shimshon 99785     ISRAEL


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.