Article View: comp.unix.programmer
Article #132Re: NAWK - ?Diff. printf output order when using a pipe.
From: Michael Salmon
Date: Mon, 27 Jul 1992 09:54
Date: Mon, 27 Jul 1992 09:54
86 lines
3333 bytes
3333 bytes
In article <1992Jul26.033811.21614@nsisrv.gsfc.nasa.gov>,
merritt@climate.gsfc.nasa.gov (John H. Merritt) writes:
|> Explain why the following 'nawk' program produces:
|>
|> ---- cut ----
|> This is the BEGIN section
|> a b c
|> 1 2 3
|> x y z
|> This is the END section
|> ---- cut ----
|>
|> When run as: 'nawk -f test.nawk test.dat', whereas it produces
|>
|> ---- cut test.out ----
|> a b c
|> 1 2 3
|> x y z
|> This is the BEGIN section
|> This is the END section
|> ---- cut test.out ----
|>
|> when redirected to a file, as in: 'nawk -f test.nawk > test.out'
|>
|> Here is the program:
|> ---- cut test.nawk ----
|> BEGIN {
|> printf("This is the BEGIN section\n");
|> }
|>
|> { printf("%s %s %s\n",$1,$2,$3) | "cat" }
|>
|> END {
|> close("cat")
|> printf("This is the END section\n");
|> }
|> ---- cut test.nawk ----
|>
|> Oh, if it isn't obvious, the contents of test.dat are:
|> -- cut test.dat --
|> a b c
|> 1 2 3
|> x y z
|> -- cut test.dat --
|>
|> Now, I guess that there must be some sort of buffering of output
|> akin to that of the beginner C programming problem of missing '\n' in
|> printf statements. I would like to have the contents of the file
|> that I redirect the output to to be identical to that of the terminal;
|> that's the confusing part -- they aren't the same.
|>
|> There is no problem when the pipe is removed.
|>
|> I want the 'BEGIN' prints to happen before the body prints and
|> the 'END' prints to happen last -- the order that I programmmed them.
|> If it is a simple matter of the right 'close' statement, then please
|> let me know what it is; I've tried 'close(1)' and 'close(stdout)'.
|>
|> Computer: IRIS 4D/70GT workstation.
|> OP/SYS : IRIX 4.0.1
|> Program : nawk
My best guess is the problem is caused by the change in buffering when
going from tty output to file/pipe output. In the first case the
newline terminates the buffering for stdout, the close of the pipe
causes nawk to wait for cat to complete and then the final printf
prints the final line. In the second case the first printf is buffered
(1k? buffer), the close of the pipe causes nawk to wait, the final
printf is buffered and finally the buffer is flushed on nawk's exit.
There are 2 solutions as I see it, i) do something tricky with pty's to
trick nawk's stdio buffers or ii) pipe the BEGIN section into cat and
then close the pipe in BEGIN, I don't think that this should be
required in the END section as this text has always been last. I don't
really like either solution but other than saying try perl I can't
think of anything else.
--
Michael Salmon
#include <standard.disclaimer>
#include <witty.saying>
#include <fancy.pseudo.graphics>
Ericsson Telecom AB
Stockholm
Technical Details
Message-ID:
<1992Jul27.095458.23787@ericsson.se>Path:
arc1-ca.pugleaf.net!sparky!uunet!mcsun!sunic!ericom!eos.ericsson.se!etxmesaReferences:
<1992Jul26.033811.21614@nsisrv.gsfc.nasa.gov>Show Full Headers
Complete Article Headers
Newsgroups: comp.unix.programmer Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!sunic!ericom!eos.ericsson.se!etxmesa From: etxmesa@eos.ericsson.se (Michael Salmon) Subject: Re: NAWK - ?Diff. printf output order when using a pipe. Message-ID: <1992Jul27.095458.23787@ericsson.se> Sender: news@ericsson.se Nntp-Posting-Host: eos6c02.ericsson.se Reply-To: etxmesa@eos.ericsson.se (Michael Salmon) Organization: Ericsson Telecom AB References: <1992Jul26.033811.21614@nsisrv.gsfc.nasa.gov> Date: Mon, 27 Jul 1992 09:54:58 GMT Lines: 86