home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / programm / 3930 < prev    next >
Encoding:
Text File  |  1992-07-27  |  3.2 KB  |  100 lines

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!mcsun!sunic!ericom!eos.ericsson.se!etxmesa
  3. From: etxmesa@eos.ericsson.se (Michael Salmon)
  4. Subject: Re: NAWK - ?Diff. printf output order when using a pipe.
  5. Message-ID: <1992Jul27.095458.23787@ericsson.se>
  6. Sender: news@ericsson.se
  7. Nntp-Posting-Host: eos6c02.ericsson.se
  8. Reply-To: etxmesa@eos.ericsson.se (Michael Salmon)
  9. Organization: Ericsson Telecom AB
  10. References:  <1992Jul26.033811.21614@nsisrv.gsfc.nasa.gov>
  11. Date: Mon, 27 Jul 1992 09:54:58 GMT
  12. Lines: 86
  13.  
  14. In article <1992Jul26.033811.21614@nsisrv.gsfc.nasa.gov>, 
  15. merritt@climate.gsfc.nasa.gov (John H. Merritt) writes:
  16. |> Explain why the following 'nawk' program produces:
  17. |> 
  18. |> ---- cut ----
  19. |> This is the BEGIN section
  20. |> a b c
  21. |> 1 2 3
  22. |> x y z
  23. |> This is the END section
  24. |> ---- cut ----
  25. |> 
  26. |> When run as: 'nawk -f test.nawk test.dat', whereas it produces
  27. |> 
  28. |> ---- cut test.out ----
  29. |> a b c
  30. |> 1 2 3
  31. |> x y z
  32. |> This is the BEGIN section
  33. |> This is the END section
  34. |> ---- cut test.out ----
  35. |> 
  36. |> when redirected to a file, as in: 'nawk -f test.nawk > test.out'
  37. |> 
  38. |> Here is the program:
  39. |> ---- cut test.nawk ----
  40. |> BEGIN {
  41. |>     printf("This is the BEGIN section\n");
  42. |> }
  43. |> 
  44. |>     { printf("%s %s %s\n",$1,$2,$3) | "cat" }
  45. |> 
  46. |> END {
  47. |>     close("cat")
  48. |>     printf("This is the END section\n");
  49. |> }
  50. |> ---- cut test.nawk ----
  51. |> 
  52. |> Oh, if it isn't obvious, the contents of test.dat are:
  53. |> -- cut test.dat --
  54. |> a b c
  55. |> 1 2 3
  56. |> x y z
  57. |> -- cut test.dat --
  58. |> 
  59. |> Now, I guess that there must be some sort of buffering of output
  60. |> akin to that of the beginner C programming problem of missing '\n' in
  61. |> printf statements.  I would like to have the contents of the file
  62. |> that I redirect the output to to be identical to that of the terminal;
  63. |> that's the confusing part -- they aren't the same.
  64. |> 
  65. |> There is no problem when the pipe is removed.
  66. |> 
  67. |> I want the 'BEGIN' prints to happen before the body prints and
  68. |> the 'END' prints to happen last -- the order that I programmmed them.
  69. |> If it is a simple matter of the right 'close' statement, then please
  70. |> let me know what it is; I've tried 'close(1)' and 'close(stdout)'.
  71. |> 
  72. |> Computer: IRIS 4D/70GT workstation.
  73. |> OP/SYS  : IRIX 4.0.1
  74. |> Program : nawk
  75.  
  76. My best guess is the problem is caused by the change in buffering when
  77. going from tty output to file/pipe output. In the first case the
  78. newline terminates the buffering for stdout, the close of the pipe
  79. causes nawk to wait for cat to complete and then the final printf
  80. prints the final line. In the second case the first printf is buffered
  81. (1k? buffer), the close of the pipe causes nawk to wait, the final
  82. printf is buffered and finally the buffer is flushed on nawk's exit.
  83. There are 2 solutions as I see it, i) do something tricky with pty's to
  84. trick nawk's stdio buffers or ii) pipe the BEGIN section into cat and
  85. then close the pipe in BEGIN, I don't think that this should be
  86. required in the END section as this text has always been last. I don't
  87. really like either solution but other than saying try perl I can't
  88. think of anything else.
  89.  
  90. -- 
  91.  
  92. Michael Salmon
  93.  
  94. #include    <standard.disclaimer>
  95. #include    <witty.saying>
  96. #include    <fancy.pseudo.graphics>
  97.  
  98. Ericsson Telecom AB
  99. Stockholm
  100.