Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this message
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.
Post Follow-up to this messageOn 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
Post Follow-up to this messageIn 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?
Post Follow-up to this message
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.
Post Follow-up to this messageOn 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
Post Follow-up to this messageOn 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.
Post Follow-up to this messageEd 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
Post Follow-up to this messageOn 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.
Post Follow-up to this messageEd 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.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.