简体   繁体   中英

(g)awk seems to cache output of shell commands - how to avoid that?

I have a weird problem with gawk and I don't know if it's a bug or awk's default behavior that I just don't understand. Basically when I execute a shell command in an awk script and capture its output with getline , all consecutive calls of that command seem to yield the same output as the first one. Here's an example:

# <foo.awk 

BEGIN {
  for(i = 0; i < 5; i++) {
    "date +%s" | getline sec
    print sec
    system("sleep 1")
  }
}
# gawk -f foo.awk
1436437519
1436437519
1436437519
1436437519
1436437519
# 

I'd like to know whether this behavior is intentional and explained somewhere in a manual (I couldn't find anything) and if there's a way to work around it.

You have to close() the pipe when you are done with each execution of the command:

$ cat foo.awk
BEGIN {
  cmd = "date +%s"
  for(i = 0; i < 5; i++) {
    cmd | getline sec
    print sec
    close(cmd)
    system("sleep 1")
  }
}

$ awk -f foo.awk
1436451378
1436451379
1436451380
1436451381
1436451382

Make sure you read and fully understand the sections on getline in the book Effective Awk Programming, 4th Edition, by Arnold Robbins (especially http://www.gnu.org/software/gawk/manual/gawk.html#Getline_002fVariable_002fPipe ) AND fully understand everything at http://awk.info/?tip/getline if you are considering using getline and then usually you'll realize there's a better approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM