Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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

Report this thread to moderator Post Follow-up to this message
Old Post
Boltar
03-28-08 12:00 AM


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.


Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
03-28-08 12:00 AM


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



Report this thread to moderator Post Follow-up to this message
Old Post
Boltar
03-28-08 12:00 AM


Re: How can I check if a file exists in gawk?
In article <d25a957c-8e1f-42ff-a8b8-29e6eb1607e2@h11g2000prf.googlegroups.co
m>,
Boltar  <boltar2003@yahoo.co.uk> wrote:
>On Mar 27, 4:36 pm, Ed Morton <mor...@lsupcaemnt.com> wrote: 
>
>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?


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
03-28-08 12:00 AM


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



Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
03-28-08 12:00 AM


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


Report this thread to moderator Post Follow-up to this message
Old Post
Boltar
03-28-08 12:00 AM


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



Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
03-28-08 12:00 AM


Re: How can I check if a file exists in gawk?
Ed Morton schrieb:
 
>
>That does not mean you need to use getline in the BEGIN section. The typica
l 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

Report this thread to moderator Post Follow-up to this message
Old Post
Michael Jaritz
03-28-08 12:00 AM


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

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



Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
03-28-08 12:00 AM


Re: How can I check if a file exists in gawk?
Ed Morton wrote:
>
>
> On 3/27/2008 11:48 AM, Boltar wrote: 

<snip>
 
>
> 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 befo
re
> 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.

Report this thread to moderator Post Follow-up to this message
Old Post
stan
03-28-08 08:59 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:24 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.