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 5 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 27, 2008 at 12:00 PM

On 3/27/2008 11:48 AM, Boltar wrote:
> On Mar 27, 4:36 pm, Ed Morton <mor...@[EMAIL PROTECTED]
> wrote:
> 
>>On 3/27/2008 11:27 AM, Boltar wrote:
>>
>>
>>>Hi
>>
>>>I'm writing an awk script that takes a path as a variable using -v. In
>>>the BEGIN section I need to parse
>>>this file using:
>>
>>>while(getline < path)
>>>{
>>>:
>>>:
>>>}
>>
>>Why? There may be a better approach that doesn't involve getline.
> 
> 
> Why? Err , because I have to read in the file line by line to extract
> the fields to set up some mappings before I start processing the main
> file in the { } section.

That does not mean you need to use getline in the BEGIN section. The
typical way
to do that instead is:

awk '
NR==FNR{ "populate mappings"; next }
{ "main file processing" }
' mappingFile mainFile

> Anyway , turns out if I do
> 
> while((getline < path) > 0)

Yes, that is almost (but not quite) one of the safe ways of invoking
getline IF
absolutely necessary to do so.

> then it doesn't hang if the path is invalid. No idea why since the
> while() should exit on a zero value anyway without the comparison
> check

The while() would exit on a zero value but getline returns -1, not zero,
if it
can't open a file or has any other input error. It only returns zero on
end of file.

> but I guess that just another one of awks irritating little quirks.

It is not an awk quirk, it's one of the many getline features that aren't
intuitively obvious and all of which need to be thoroughly understood
before
deciding whether or not to use getline.

	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:53:33 CDT 2008.