How can I check if a file exists in gawk?

This is a discussion on How can I check if a file exists in gawk? within the awk forums in Programming Languages category; 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) { : : } Problem is , if the file doesn't exist it just hangs. How can I check if the file exists in the first place? I'd rather avoid using system() if possible as the script needs to be OS portable. Thanks for any help B2003...

Go Back   Application Development Forum > Programming Languages > awk

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 03-27-2008, 12:27 PM
Boltar
Guest
 
Default How can I check if a file exists in gawk?

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)
{
:
:
}

Problem is , if the file doesn't exist it just hangs. How can I check
if the file exists in the first place? I'd
rather avoid using system() if possible as the script needs to be OS
portable.

Thanks for any help

B2003
Reply With Quote
  #2  
Old 03-27-2008, 12:36 PM
Ed Morton
Guest
 
Default Re: How can I check if a file exists in gawk?



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.

> Problem is , if the file doesn't exist it just hangs. How can I check
> if the file exists in the first place?


See http://tinyurl.com/yn9ka9.

Ed.

Reply With Quote
  #3  
Old 03-27-2008, 12:48 PM
Boltar
Guest
 
Default Re: How can I check if a file exists in gawk?

On Mar 27, 4:36 pm, Ed Morton <mor...@lsupcaemnt.com> 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.

Anyway , turns out if I do

while((getline < path) > 0)

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 but I guess that just another one of awks irritating little
quirks.

B2003


Reply With Quote
  #4  
Old 03-27-2008, 12:54 PM
Kenny McCormack
Guest
 
Default Re: How can I check if a file exists in gawk?

In article <d25a957c-8e1f-42ff-a8b8-29e6eb1607e2@h11g2000prf.googlegroups.com>,
Boltar <boltar2003@yahoo.co.uk> wrote:
>On Mar 27, 4:36 pm, Ed Morton <mor...@lsupcaemnt.com> 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.


There's another way...

>Anyway , turns out if I do
>
>while((getline < path) > 0)
>
>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 but I guess that just another one of awks irritating little
>quirks.


I guess it is a "quirk" that numbers can be negative, isn't it?

Reply With Quote
  #5  
Old 03-27-2008, 01:00 PM
Ed Morton
Guest
 
Default Re: How can I check if a file exists in gawk?



On 3/27/2008 11:48 AM, Boltar wrote:
> On Mar 27, 4:36 pm, Ed Morton <mor...@lsupcaemnt.com> 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.


Reply With Quote
  #6  
Old 03-27-2008, 01:04 PM
Boltar
Guest
 
Default Re: How can I check if a file exists in gawk?

On Mar 27, 4:54 pm, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:

> I guess it is a "quirk" that numbers can be negative, isn't it?


Oops, you're right, should have spotted that. Though the first guys
answer wasn't exactly helpful.

B2003

Reply With Quote
  #7  
Old 03-27-2008, 02:25 PM
Ed Morton
Guest
 
Default Re: How can I check if a file exists in gawk?



On 3/27/2008 12:04 PM, Boltar wrote:
> On Mar 27, 4:54 pm, gaze...@xmission.xmission.com (Kenny McCormack)
> wrote:
>
>
>>I guess it is a "quirk" that numbers can be negative, isn't it?

>
>
> Oops, you're right, should have spotted that. Though the first guys
> answer wasn't exactly helpful.


As the old adage goes: If you give a hungry man a fish, he'll eat today, but if
you TEACH a hungry man to fish.... apparently he'll say "that wasn't exactly
helpful - gimme a fish!".

Sigh...

Ed.


Reply With Quote
  #8  
Old 03-27-2008, 06:36 PM
Michael Jaritz
Guest
 
Default Re: How can I check if a file exists in gawk?

Ed Morton schrieb:

>>>>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


What is the typical way to do this if the both files needs different FS
or different FIELDWIDTHS?

Michael
Reply With Quote
  #9  
Old 03-27-2008, 06:56 PM
Ed Morton
Guest
 
Default Re: How can I check if a file exists in gawk?



On 3/27/2008 5:36 PM, Michael Jaritz wrote:
> Ed Morton schrieb:
>
>
>>>>>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

>
>
> What is the typical way to do this if the both files needs different FS
> or different FIELDWIDTHS?


Set those variables between the files, e.g.:

awk '...' file1 FS='\t' file2

Ed.


Reply With Quote
  #10  
Old 03-27-2008, 11:05 PM
stan
Guest
 
Default Re: How can I check if a file exists in gawk?

Ed Morton wrote:
>
>
> On 3/27/2008 11:48 AM, Boltar wrote:
>> On Mar 27, 4:36 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
>>
>>>On 3/27/2008 11:27 AM, Boltar wrote:


<snip>

>> 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.


Personally, I found that I needed a firm fixed rule that every time I
type getline I have to get up and go get a cup of coffee. Without any
exceptions that I can remember I always find that I was trying to write
a c progrm in awk and with a little reflection I can find a solution
that uses awk instead of fighting against it.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 03:49 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.