How can I check if a file exists in gawk? - awk

This is a discussion on How can I check if a file exists in gawk? - awk ; 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 ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 17

How can I check if a file exists in gawk?

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

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


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



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


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



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


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



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

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



  10. 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 to Thread
Page 1 of 2 1 2 LastLast