Article View: comp.unix.programmer
Article #127NAWK - ?Diff. printf output order when using a pipe.
From: John H. Merritt
Date: Sun, 26 Jul 1992 03:38
Date: Sun, 26 Jul 1992 03:38
66 lines
2179 bytes
2179 bytes
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
--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
John H. Merritt --> merritt@climate.gsfc.nasa.gov
"I am generally intolerant of ignorance,
but I have made an exception in your case."
Technical Details
Message-ID:
<1992Jul26.033811.21614@nsisrv.gsfc.nasa.gov>Path:
arc1-ca.pugleaf.net!sparky!uunet!cs.utexas.edu!sun-barr!ames!nsisrv!climate.gsfc.nasa.gov!merrittShow Full Headers
Complete Article Headers
Newsgroups: comp.unix.programmer Path: arc1-ca.pugleaf.net!sparky!uunet!cs.utexas.edu!sun-barr!ames!nsisrv!climate.gsfc.nasa.gov!merritt From: merritt@climate.gsfc.nasa.gov (John H. Merritt) Subject: NAWK - ?Diff. printf output order when using a pipe. Message-ID: <1992Jul26.033811.21614@nsisrv.gsfc.nasa.gov> Sender: usenet@nsisrv.gsfc.nasa.gov (Usenet) Nntp-Posting-Host: climate.gsfc.nasa.gov Organization: Climate and Radiation Branch Date: Sun, 26 Jul 1992 03:38:11 GMT Lines: 66