Problems with getline in cygwin awk 
Author Message
 Problems with getline in cygwin awk

DUH!!
ignore this, it helps if you define 'f' before trying to close it
Not enough sleep, not enough caffine :)
But strangely it does seem that
BEGIN {
for (i=1;i<10;i++){
while((getline < "hello.txt")>0){
  print $0
Quote:
}

  close("hello.txt")
Quote:
}
}

works but END{....[same code] doesn't.

Is this just me being stupid again??

chris

Quote:

> On cygwin b20 running gawk 3.0.3:-

> BEGIN {
> for (i=1;i<10;i++){
> while((getline < "hello.txt")>0){
>   print $0
> }
>   close(f)
> }
> }

> [snip]



Sat, 03 Nov 2001 03:00:00 GMT  
 Problems with getline in cygwin awk
Hmm, I get the same with Cygwin b20.

Two solutions (neither of which are great):
1) Try compiling your script using awka (available from
http://www.geocities.com/TimesSquare/Bunker/1347/awka.html ) which seems to
close files ok. but may have other bugs in it (its still in beta)
2) Stop using cygwin and use mawk from within dos instead (the FAQ will tell
you were to get it from, I cant remember). Which closes files properly, but
has problems with unix file formats.

I guess it depends what you're trying to do

chris

Quote:

> Hello,

> I have the problem that I have to read one file multiple times in an awk
> script
> for cross referencing.

> If I run my script on UNIX or POSIX shell the script will run well.

> In CYGWIN awk the program run the first loop over the file and then it
> don't read
> the file anymore. I do a close after every loop over the file.
> (For me, it seems to be a bug in the close function)

> Did anyone have a solution for this problem.

> I use CYGWIN Beta 20.1

> Regards
> Wolfgang Hauser



Sat, 03 Nov 2001 03:00:00 GMT  
 Problems with getline in cygwin awk

Quote:

> I have the problem that I have to read one file multiple times in an awk
> script
> for cross referencing.

I cannot reproduce the problem here.
Does this work on your system ?

BEGIN {
  f = "test.asc"
  getline < f
  print $0
  close(f)
  getline < f
  print $0
  close(f)

Quote:
}

When called from the DOS prompt, this script printed the first line
of the file twice. I have to adimt that I used GAWK 3.0.42 and
3.1.80 with Cygwin only, because I did not have others at hand.

+---------------------------------------------------------------------+
| Juergen Kahrs,       STN Atlas Elektronik GmbH,   D-28305 Bremen    |
| Simulation Division  Sebaldsbruecker Heerstr. 235 +49/421/457-2819  |
+----------- http://home.t-online.de/home/Juergen.Kahrs/ -------------+



Sat, 03 Nov 2001 03:00:00 GMT  
 Problems with getline in cygwin awk
On cygwin b20 running gawk 3.0.3:-

BEGIN {
for (i=1;i<10;i++){
while((getline < "hello.txt")>0){
  print $0

Quote:
}
  close(f)
}
}

refuses to close the file and so prints the contents of hello.txt just once

BEGIN {
f="hello.txt"
for (i=1;i<10;i++){
while((getline < f)>0){
  print $0

Quote:
}
  close(f)
}
}

works as advertised (printing the contents of hello.txt 9 times)

3.0.3 seems to be the newest version available on ftp://ftp.gnu.org/gnu/gawk/
so it looks like you'll have to work round it for now.

chris

Quote:


> > I have the problem that I have to read one file multiple times in an awk
> > script
> > for cross referencing.

> I cannot reproduce the problem here.
> Does this work on your system ?

> BEGIN {
>   f = "test.asc"
>   getline < f
>   print $0
>   close(f)
>   getline < f
>   print $0
>   close(f)
> }

> When called from the DOS prompt, this script printed the first line
> of the file twice. I have to adimt that I used GAWK 3.0.42 and
> 3.1.80 with Cygwin only, because I did not have others at hand.

> +---------------------------------------------------------------------+
> | Juergen Kahrs,       STN Atlas Elektronik GmbH,   D-28305 Bremen    |
> | Simulation Division  Sebaldsbruecker Heerstr. 235 +49/421/457-2819  |
> +----------- http://home.t-online.de/home/Juergen.Kahrs/ -------------+



Sat, 03 Nov 2001 03:00:00 GMT  
 Problems with getline in cygwin awk
Partly...  How do you invoke it?

If you use:

        gawk -f hello.awk

then it expects input from standard input.

If you do:

        gawk -f hello.awk hello.txt

Then hello.txt is still open and the loop
only works once.

If you do:

        gawk -f hello.awk hello.awk

It works fine...

--
Best regards,
 _ __                      _    ,   _ _ _
' )  )     /         _/_  ' )  /   ' ) ) )
 /--' ____/___/> __  /     /--/     / / / __,_  __  o _   ______
/  \_(_) /_) (__/ (_<__   /  ( o   / ' (_(_) (_/ (_<_/_)_(_) / <_

Robert H. Morrison                      Tel:   +49 721 9628 167
Software Development, Basis Team        FAX:   +49 721 9628 149



Quote:
> DUH!!
> ignore this, it helps if you define 'f' before trying to close it
> Not enough sleep, not enough caffine :)
> But strangely it does seem that
> BEGIN {
> for (i=1;i<10;i++){
> while((getline < "hello.txt")>0){
>   print $0
> }
>   close("hello.txt")
> }
> }
> works but END{....[same code] doesn't.

> Is this just me being stupid again??

> chris


> > On cygwin b20 running gawk 3.0.3:-

> > BEGIN {
> > for (i=1;i<10;i++){
> > while((getline < "hello.txt")>0){
> >   print $0
> > }
> >   close(f)
> > }
> > }

> > [snip]



Sun, 04 Nov 2001 03:00:00 GMT  
 Problems with getline in cygwin awk


   >If you use:
   >gawk -f hello.awk
   >then it expects input from standard input.
    > BEGIN {
    > for (i=1;i<10;i++){
    > while((getline < "hello.txt")>0){
    >   print $0
    > }
    >   close(f)
    > }
exit
    > }

Net-Tamer V 1.08X - Test Drive



Mon, 05 Nov 2001 03:00:00 GMT  
 Problems with getline in cygwin awk
That's exactly my problem too, I do my loop in the END segment of my
script.

May be i have t write a bug report to the cygwin guys.

Wolfgang Hauser

Quote:

> DUH!!
> ignore this, it helps if you define 'f' before trying to close it
> Not enough sleep, not enough caffine :)
> But strangely it does seem that
> BEGIN {
> for (i=1;i<10;i++){
> while((getline < "hello.txt")>0){
>   print $0
> }
>   close("hello.txt")
> }
> }
> works but END{....[same code] doesn't.

> Is this just me being stupid again??

> chris


> > On cygwin b20 running gawk 3.0.3:-

> > BEGIN {
> > for (i=1;i<10;i++){
> > while((getline < "hello.txt")>0){
> >   print $0
> > }
> >   close(f)
> > }
> > }

> > [snip]



Tue, 06 Nov 2001 03:00:00 GMT  
 Problems with getline in cygwin awk

Quote:

> That's exactly my problem too, I do my loop in the END segment of my
> script.

I just tested the given script again and it worked.
The problem you have is caused by this script:

END {
  for (i=1;i<=10;i++){
    while ((getline < "xxx") > 0) {
      print i, $0
    }
    close("xxx")
  }

Quote:
}

This script will, of course, wait for input to arrive on stdin.
Therefore, it *must* behave different from the other one.

Quote:

> May be i have t write a bug report to the cygwin guys.

> Wolfgang Hauser

Can you really reproduce the problem on another Unix ?
The Cygwin people do not change the GNU AWK tool. They
install it out of the box. I would be surprised if such
an elementary problem would be unique to the Cygwin tools.

+---------------------------------------------------------------------+
| Juergen Kahrs,       STN Atlas Elektronik GmbH,   D-28305 Bremen    |
| Simulation Division  Sebaldsbruecker Heerstr. 235 +49/421/457-2819  |
+----------- http://home.t-online.de/home/Juergen.Kahrs/ -------------+



Tue, 06 Nov 2001 03:00:00 GMT  
 Problems with getline in cygwin awk
Here is the answer for the problem !

Thanks to all.

Quote:

> That's exactly my problem too, I do my loop in the END segment of my
> script.

> May be i have t write a bug report to the cygwin guys.

It looks like you have found a real bug in GNU AWK.
The problem also exists on my IRIX and Linux machines.
It is at least a difference in behaviour between GNU AWK and
all other AWKs I have.

Just in case someone else wants to chase the bug:

BEGIN { f=ARGV[1] }
END {
  while ( (getline < f) > 0 ) print 1, $0 ; close(f);
  while ( (getline < f) > 0 ) print 2, $0 ; close(f);
  while ( (getline < f) > 0 ) print 3, $0 ; close(f);

Quote:
}

A necessary condition of the appearance of the bug is the END
pattern.

This script should print the contents of the file three times.
But GNU AWK prints it only once. I did tests with versions 3.0.3,
3.0.44 and 3.1.80.

| Juergen Kahrs,       STN Atlas Elektronik GmbH|



Tue, 06 Nov 2001 03:00:00 GMT  
 Problems with getline in cygwin awk
Although your problem has been solved, I found another funny (?) behaviour
when I removed your BEGIN clause:

elmicha$ cat x.awk
END {
  while ( (getline < f) > 0 ) print 1, $0 ; close(f);
  while ( (getline < f) > 0 ) print 2, $0 ; close(f);
  while ( (getline < f) > 0 ) print 3, $0 ; close(f);

Quote:
}

elmicha$ gawk -v f=x.awk -f x.awk x.awk
?abc def should be ?abc
?" abc "def should be ? abc
?"abc def  could be ?abc def
?"abc def" should be ?abc def
?"abc" def should be ?abc
?"abc\" def" should be ?abc\

Strange. gawk 3.0.3 on Linux.
It's only an academic issue, but I really would like to understand what's
going here.

Regards...
        Michael



Tue, 06 Nov 2001 03:00:00 GMT  
 Problems with getline in cygwin awk

Quote:

> Strange. gawk 3.0.3 on Linux.
> It's only an academic issue, but I really would like to understand what's
> going here.

When I tried it (gawk 3.0.3 on Linux 2.2.5), I got:

1 END {
1   while ( (getline < f) > 0 ) print 1, $0 ; close(f);
1   while ( (getline < f) > 0 ) print 2, $0 ; close(f);
1   while ( (getline < f) > 0 ) print 3, $0 ; close(f);
1 }

So, I cannot reproduce *your* problem but the other one.
Anyway, on 19. May in gnu.utils.bug, Arnold Robbins posted a patch
which addressed a very similar problem. I have not tested it, but
maybe it helps:

Quote:
> This is indeed a bug. It occurs when the current (or last) file named on the
> command line is also used as the subject of a redirection.  (I don't see
> anything in your code that would print the actual numbers, nor do I get
> that result.)

> In any case, this unofficial patch should fix the problem.  (Your line
> numbers will vary.)

> Please let me know if it does not.

> Arnold

-----------------------------------------------------
*** ../gawk-3.0.3/io.c  Wed May 14 15:10:02 1997
--- io.c        Wed May 19 14:02:05 1999
***************
*** 573,596 ****

        tmp = force_string(tree_eval(tree->subnode));

-       /* icky special case: close(FILENAME) called. */
-       if (tree->subnode == FILENAME_node
-           || (tmp->stlen == FILENAME_node->var_value->stlen
-               && STREQN(tmp->stptr, FILENAME_node->var_value->stptr,
tmp->stlen))) {
-               (void) nextfile(TRUE);
-               free_temp(tmp);
-               return tmp_number((AWKNUM) 0.0);
-       }
-
        for (rp = red_head; rp != NULL; rp = rp->next) {
                if (strlen(rp->value) == tmp->stlen
                    && STREQN(rp->value, tmp->stptr, tmp->stlen))
                        break;
        }
        if (rp == NULL) {       /* no match */
!               if (do_lint)
                        warning("close: `%.*s' is not an open file or pipe",
                                tmp->stlen, tmp->stptr);
                free_temp(tmp);
                return tmp_number((AWKNUM) 0.0);
        }
--- 582,603 ----

        tmp = force_string(tree_eval(tree->subnode));

        for (rp = red_head; rp != NULL; rp = rp->next) {
                if (strlen(rp->value) == tmp->stlen
                    && STREQN(rp->value, tmp->stptr, tmp->stlen))
                        break;
        }
+
        if (rp == NULL) {       /* no match */
!               /* icky special case: close(FILENAME) called. */
!               if (tree->subnode == FILENAME_node
!                   || (tmp->stlen == FILENAME_node->var_value->stlen
!                       && STREQN(tmp->stptr, FILENAME_node->var_value->stptr,
tmp->stlen))) {
!                       (void) nextfile(TRUE);
!               } else if (do_lint)
                        warning("close: `%.*s' is not an open file or pipe",
                                tmp->stlen, tmp->stptr);
+
                free_temp(tmp);
                return tmp_number((AWKNUM) 0.0);
        }

+---------------------------------------------------------------------+
| Juergen Kahrs,       STN Atlas Elektronik GmbH,   D-28305 Bremen    |
| Simulation Division  Sebaldsbruecker Heerstr. 235 +49/421/457-2819  |
+----------- http://home.t-online.de/home/Juergen.Kahrs/ -------------+



Sat, 10 Nov 2001 03:00:00 GMT  
 Problems with getline in cygwin awk

Quote:

> When I tried it (gawk 3.0.3 on Linux 2.2.5), I got:

> 1 END {
> 1   while ( (getline < f) > 0 ) print 1, $0 ; close(f);
> 1   while ( (getline < f) > 0 ) print 2, $0 ; close(f);
> 1   while ( (getline < f) > 0 ) print 3, $0 ; close(f);
> 1 }

> So, I cannot reproduce *your* problem but the other one.

Oh, shame on me: after patching and recompiling gawk, debugging gawk,
getting more and more paranoid, I found my problem:

I had AWKPATH set to "/dos/Links/awkdir", and in that directory lived an
"x.awk", just the same name I used for testing now.

Thank you for forwarding the patch, now I finally got the same results
as you did (without the patch only the first loop, with it all three).

Regards...
                Michael



Sat, 10 Nov 2001 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. awk lookup problem using getline

2. awk: getline of a file

3. memory problem with cygwin/gfortran and cygwin/g77

4. Cygwin, tcl and awk

5. problem with multiple getline

6. problem with multiple getline

7. newbie help - getline problem

8. Another Getline problem

9. getline problems

10. getline problem

11. quoting problem with piping command to getline

12. Getline problems with Lookup file

 

 
Powered by phpBB® Forum Software