Elapsed time in seconds in awk

I am trying to get the ellapsed time in seconds in the body of the awk script. I use unix date to get the time. It works in BEGIN {} but not in the body {} of awk. Any ideas?

$ cat a
BEGIN {
  "date +%s" | getline x
  print x
}
{
  "date +%s" | getline y
  print y
}
$ echo "one line" | awk -f a
1286442885

$

in awk, if you need run shell command:

system("date +%s")

does the system command allow to capture the output of it? Eventually I would like to subtract the values: y-x in the script. This script, obviously, does not work:

BEGIN {
  system("date +%s") | getline x
  print x
}
{
  system("date +%s") | getline y
  print y
}

Ok, use systime function in awk.

cat a

BEGIN {
print systime()
}
{
print systime()
}
1 Like