Use getline inside loop

awk, while-loop

Solution

$ cat tst.awk
BEGIN {
    cmd = "date"
    while (!done) {
        if ( (cmd | getline foo) > 0 ) {
            print foo
            done = (++i == 5 ? 1 : 0)
        }
        else {
            done = 1
        }
        close(cmd)
    }
}
$ awk -f tst.awk
Fri, Nov 28, 2014  3:18:21 PM
Fri, Nov 28, 2014  3:18:21 PM
Fri, Nov 28, 2014  3:18:21 PM
Fri, Nov 28, 2014  3:18:21 PM
Fri, Nov 28, 2014  3:18:21 PM

or if you prefer (but this is a potentially infinite loop):

BEGIN {
    cmd = "date"
    while ( (cmd | getline foo) > 0 ) {
        print foo
        close(cmd)
    }
}

Here is a stopwatch in GNU awk:

$ cat tst.awk
/s/ { start = systime() }
/e/ { end = systime(); print "elapsed:", end - start, "secs\n" }
/x/ { exit }
$
$ awk -f tst.awk
s
e
elapsed: 2 secs

s
e
elapsed: 6 secs

x

Here's how to do what your bash script (https://superuser.com/a/694393) is doing:

$ cat tst.awk
BEGIN {
    cmd = "date +%s.%N"
    if  ( (cmd | getline x) > 0 ) {
        close(cmd)
        while ( (cmd | getline y) > 0 ) {
            close(cmd)
            printf "%s\r", y-x
            if (++i == 10) exit
        }
    }
}
$
$ awk -f tst.awk
$ 7176013

I don't know what the second `date` in your shell command is doing but I figure you can figure that part out and code it in awk or set up a `cmd2` variable to call date again if necessary.

Oh, what the heck:

BEGIN {
    date_sN = "date +%s.%N"
    date_TN_start = "date +%T.%N -ud@"

    if  ( (date_sN | getline x) > 0 ) {
        close(date_sN)
        while ( (date_sN | getline y) > 0 ) {
            close(date_sN)
            date_TN = sprintf("%s%.11f", date_TN_start, y - x)
            if  ( (date_TN | getline d) > 0 ) {
                close(date_TN)
                printf "%s\r", d
            }
            if (++i == 10) exit
        }
    }
}

Problem

Consider this script ``` #!awk -f BEGIN { "date" | getline foo print foo } ``` It will print the current date, as expected. However if you put it in a loop ``` #!awk -f BEGIN { while (1) { "date" | getline foo printf "%s\r", foo } } ``` it just prints the same date repeatedly. I would like to capture external command in a loop using either `getline` or `system`

Original source