home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / programm / 3960 < prev    next >
Encoding:
Internet Message Format  |  1992-07-28  |  2.8 KB

  1. Path: sparky!uunet!mcsun!uknet!acorn!ixi!ixi!pd
  2. From: pd@x.co.uk (Paul Davey)
  3. Newsgroups: comp.unix.programmer
  4. Subject: Re: NAWK - ?Diff. printf output order when using a pipe.
  5. Message-ID: <PD.92Jul28131139@herts.x.co.uk>
  6. Date: 28 Jul 92 13:11:39 GMT
  7. References: <1992Jul26.033811.21614@nsisrv.gsfc.nasa.gov>
  8.     <1992Jul26.221156.12144@nsisrv.gsfc.nasa.gov>
  9. Sender: paul@x.co.uk (Paul Davey)
  10. Organization: IXI Ltd.
  11. Lines: 65
  12. In-Reply-To: merritt@climate.gsfc.nasa.gov's message of 26 Jul 92 22:11:56 GMT
  13.  
  14. >>>>> On 26 Jul 92 22:11:56 GMT, merritt@climate.gsfc.nasa.gov (John H. Merritt) said:
  15.  
  16. John> In article <1992Jul26.033811.21614@nsisrv.gsfc.nasa.gov>, merritt@climate.gsfc.nasa.gov (John H. Merritt) writes:
  17. John> |> Explain why the following 'nawk' program produces:
  18. John> [ output deleted ]
  19.  
  20. John> Already there is misunderstanding about what I want in the way of answers.
  21. John> I want a solution to getting the same output when seeing the output
  22. John> at the terminal or redirecting it to a file.  
  23. John> I WANT THE PIPE TO REMAIN.
  24.  
  25. Then your solution is to pipe everything through the pipe and it will
  26. come out at the same time.  (Unfortunately you don't want to sort your
  27. begin and end parts though.)
  28.  
  29. Your problem is illustrated here as a data flow diagram
  30.  
  31.           BEGIN and END ----------> OUTPUT1
  32.         /                     \
  33. data -> [awk]                      > terminal output
  34.         \ MIDDLE --------> [cat] -> OUTPUT2 /
  35.  
  36. As soon as you have two processes (when awk forks the cat process as a
  37. child) the branches get out of step.
  38.  
  39. Sort will take a long time to run as it has to read all its input
  40. before starting to output. 
  41.  
  42.  
  43. John> If there is something like 'fflush' in AWK, that would be great.
  44.  
  45. There isn't. Besides awk isn't producing the output of sort. 
  46.  
  47. John> So far 'close' was the closest thing I found, but the exact syntax eludes
  48. John> me -- if it is the right thing at all.
  49.  
  50. That lets you close a file, but I don't think you can close a pipe.
  51.  
  52. I think you will have to settle for three lots of output, (BEGIN,
  53. MIDDLE and END say) and join them together when the all processes have
  54. exited.
  55.  
  56. Awk might exit before the cat or sort pipeline has however, so you
  57. must move the pipe outside of awk.
  58.  
  59. Make your awk program do this
  60.  
  61.     BEGIN    do begin stuff > begin.tmp
  62.  
  63.     MIDDLE  do middle stuff to standard out 
  64.  
  65.     END     do end stuff > end.tmp
  66.  
  67.  
  68. Then run it like this
  69.  
  70. nawk -f awkfile  | sort > sort.tmp && cat begin.tmp sort.tmp end.tmp > finaloutput
  71.  
  72.  
  73. John> -- 
  74. John> \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
  75. John> John H. Merritt --> merritt@climate.gsfc.nasa.gov
  76. John> "I am generally intolerant of ignorance,
  77. John> but I have made an exception in your case."
  78.  
  79.  
  80.  
  81.  
  82. --
  83.  Regards,              pd@x.co.uk           IXI Ltd.
  84.     Paul Davey          pd@ixi.uucp          62-74 Burleigh St.
  85.                   ...!uunet!ixi!pd     Cambridge,  U.K.
  86.  "These _are_ interesting times"  +44 223 462 131      CB1  1OJ
  87.