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

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!cs.utexas.edu!sun-barr!ames!nsisrv!climate.gsfc.nasa.gov!merritt
  3. From: merritt@climate.gsfc.nasa.gov (John H. Merritt)
  4. Subject: NAWK - ?Diff. printf output order when using a pipe.
  5. Message-ID: <1992Jul26.033811.21614@nsisrv.gsfc.nasa.gov>
  6. Sender: usenet@nsisrv.gsfc.nasa.gov (Usenet)
  7. Nntp-Posting-Host: climate.gsfc.nasa.gov
  8. Organization: Climate and Radiation Branch
  9. Date: Sun, 26 Jul 1992 03:38:11 GMT
  10. Lines: 66
  11.  
  12. Explain why the following 'nawk' program produces:
  13.  
  14. ---- cut ----
  15. This is the BEGIN section
  16. a b c
  17. 1 2 3
  18. x y z
  19. This is the END section
  20. ---- cut ----
  21.  
  22. When run as: 'nawk -f test.nawk test.dat', whereas it produces
  23.  
  24. ---- cut test.out ----
  25. a b c
  26. 1 2 3
  27. x y z
  28. This is the BEGIN section
  29. This is the END section
  30. ---- cut test.out ----
  31.  
  32. when redirected to a file, as in: 'nawk -f test.nawk > test.out'
  33.  
  34. Here is the program:
  35. ---- cut test.nawk ----
  36. BEGIN {
  37.     printf("This is the BEGIN section\n");
  38. }
  39.  
  40.     { printf("%s %s %s\n",$1,$2,$3) | "cat" }
  41.  
  42. END {
  43.     close("cat")
  44.     printf("This is the END section\n");
  45. }
  46. ---- cut test.nawk ----
  47.  
  48. Oh, if it isn't obvious, the contents of test.dat are:
  49. -- cut test.dat --
  50. a b c
  51. 1 2 3
  52. x y z
  53. -- cut test.dat --
  54.  
  55. Now, I guess that there must be some sort of buffering of output
  56. akin to that of the beginner C programming problem of missing '\n' in
  57. printf statements.  I would like to have the contents of the file
  58. that I redirect the output to to be identical to that of the terminal;
  59. that's the confusing part -- they aren't the same.
  60.  
  61. There is no problem when the pipe is removed.
  62.  
  63. I want the 'BEGIN' prints to happen before the body prints and
  64. the 'END' prints to happen last -- the order that I programmmed them.
  65. If it is a simple matter of the right 'close' statement, then please
  66. let me know what it is; I've tried 'close(1)' and 'close(stdout)'.
  67.  
  68. Computer: IRIS 4D/70GT workstation.
  69. OP/SYS  : IRIX 4.0.1
  70. Program : nawk
  71.  
  72.  
  73. -- 
  74. \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
  75. John H. Merritt --> merritt@climate.gsfc.nasa.gov
  76. "I am generally intolerant of ignorance,
  77. but I have made an exception in your case."
  78.