Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Intermixing GAWK print statements with calls to system()?

35 views
Skip to first unread message

Kenny McCormack

unread,
Oct 24, 2016, 8:27:13 AM10/24/16
to
The context is GAWK on Unix/Linux. Not concerned with any other environments.

Suppose you do:

$ gawk4 'BEGIN { print "Line 1";system("echo Line 2");print "Line 3"}' > file

Is this guaranteed to work as expected? I.e., the output is:

Line 1
Line 2
Line 3

I.e., the concern is buffereing - specifically, that the GAWK output might
be "fully buffered" and thus might not get flushed to the file until the
program exits.

But I think system(3) is specifically coded so that this won't happen.
But I cannot find chapter and verse at the moment.

--
A liberal, a moderate, and a conservative walk into a bar...

Bartender says, "Hello, Donald!"

Marc de Bourget

unread,
Oct 24, 2016, 8:58:56 AM10/24/16
to
Hi Kenny, I always prefer getline() instead of system().
It's more reliable:

BEGIN {
print "Line 1"
cmd = "echo Line 2"
if ((cmd | getline var) > 0) {
print var
}
close(cmd)
print "Line 3"
}

Andrew Schorr

unread,
Oct 24, 2016, 7:34:47 PM10/24/16
to
On Monday, October 24, 2016 at 8:27:13 AM UTC-4, Kenny McCormack wrote:
> Suppose you do:
>
> $ gawk4 'BEGIN { print "Line 1";system("echo Line 2");print "Line 3"}' > file
>
> Is this guaranteed to work as expected? I.e., the output is:
>
> Line 1
> Line 2
> Line 3

I refer you to the fabulous gawk documentation:

https://www.gnu.org/software/gawk/manual/html_node/I_002fO-Functions.html

which says:

If you think about what a programmer expects, it makes sense that system() should flush any pending output. The following program:

BEGIN {
print "first print"
system("echo system echo")
print "second print"
}
must print:

first print
system echo
second print
and not:

system echo
first print
second print
If awk did not flush its buffers before calling system(), you would see the latter (undesirable) output.

Kaz Kylheku

unread,
Oct 24, 2016, 8:26:15 PM10/24/16
to
On 2016-10-24, Andrew Schorr <asc...@telemetry-investments.com> wrote:
> On Monday, October 24, 2016 at 8:27:13 AM UTC-4, Kenny McCormack wrote:
>> Suppose you do:
>>
>> $ gawk4 'BEGIN { print "Line 1";system("echo Line 2");print "Line 3"}' > file
>>
>> Is this guaranteed to work as expected? I.e., the output is:
>>
>> Line 1
>> Line 2
>> Line 3
>
> I refer you to the fabulous gawk documentation:
>
> https://www.gnu.org/software/gawk/manual/html_node/I_002fO-Functions.html
>
> which says:
>
> If you think about what a programmer expects, it makes sense that
> system() should flush any pending output. The following program:

We learn other things here too:

- system("") is recommended as a portable way to flush output,
insinuating that it works on historic Awk implementations.

- the fflush() function was added by Kernighan to his maintained
awk version (I'm guessing One True Awk?) in 1992, and was taken
up by the the Austin group in 2012. A link is given to the issue:
http://austingroupbugs.net/view.php?id=634

On the other hand:

- I can find nothing in the POSIX description of Awk that the
system() function is required to have a any flushing effect. It is
described as simply executing the command in the same manner as the C
system() function.

- The POSIX description of system() is fatter than that in ISO C;
neither ISO C nor POSIX say anything about flushing.

- Four years later, The 2016 online version of POSIX makes no mention
of fflush for Awk. This issue was not integrated:
http://austingroupbugs.net/view.php?id=634

Kenny McCormack

unread,
Oct 24, 2016, 9:24:32 PM10/24/16
to
In article <c7cc4920-c397-4676...@googlegroups.com>,
Thanks. I knew I had read that text (or something very similar) somewhere.

Just couldn't put my finger on it.

Glad to see it confirms what I had assumed to be the case.

But note that the flushing is done by AWK itself - not by the interface to
system(3).

P.S. Thanks also to Kaz for his interesting and informative post on this
thread.

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain in
compliance with said RFCs, the actual sig can be found at the following web address:
http://www.xmission.com/~gazelle/Sigs/Noam

Geoff Clare

unread,
Oct 25, 2016, 8:41:04 AM10/25/16
to
Kaz Kylheku wrote:

> - the fflush() function was added by Kernighan to his maintained
> awk version (I'm guessing One True Awk?) in 1992, and was taken
> up by the the Austin group in 2012. A link is given to the issue:
> http://austingroupbugs.net/view.php?id=634
>
> On the other hand:
[...]
> - Four years later, The 2016 online version of POSIX makes no mention
> of fflush for Awk. This issue was not integrated:
> http://austingroupbugs.net/view.php?id=634

Bug 634 is tagged for inclusion in Issue 8 of the POSIX/SUS specs.
The 2016 edition is a "bug fix" update of Issue 7.

--
Geoff Clare <net...@gclare.org.uk>
0 new messages