Print date at END clause of AWK

Hi to all! I 'm new in unix programing so... may be I decided a wrong tool to solve the problem but anyway... all road goes to rome jajaja.

My question is: There is any way to print date at the END clause of an AWK script. I mean, I'm writing a tool with AWK and the results are redirected to a file.
Some thing like:

BEGIN{
"date" |getline;
print >>TestLog.log
}

These lines works "sweetly" at the BEGIN clause... but it doesn't at the END clause.
I'm working on a HP-UX (unknow version :p)

I know that this is not actually a big challenge (jajaja) but I googled as much I could without results.

How could I log the end of the AWK script execution?

Thanks matrixmadhan for you support :slight_smile:

F

Use getline var instead of getline only.

$ awk < /dev/null 'BEGIN {
  if (!("date" | getline dt))
    print "Error getting date"
  }
END {
  print "date is:", dt
  }'
date is: Fri Nov  6 12:29:53 MET 2009

Hi dude, thanks for your reply!

Actually, works like you said, but you are storing the date at the beging of the script execution. I need the date/time when the script ends.

Thanks for your reply

F

You can use something like this:

awk <<< 1  'BEGIN {
  printf "start: "; system("date")
  }
system("sleep 3")
END {
  printf "end: "; system("date") 
  }'  
zsh-4.3.10[sysadmin]% awk <<< 1  'BEGIN {
  printf "start: "; system("date")
  }
system("sleep 3")
END {
  printf "end: "; system("date")
  }'  
start: Fri Nov  6 15:38:53     2009
end: Fri Nov  6 15:38:56     2009

---------- Post updated at 03:47 PM ---------- Previous update was at 03:31 PM ----------

Actually, you just need to close the external command:

awk <<< 1  'BEGIN {
  "date" | getline dt
  close("date")
  print "start:", dt
  }
system("sleep 3")
END {
  "date" | getline dt
  close("date")
  print "end:", dt 
  }'  

Like this:

zsh-4.3.10[sysadmin]% awk <<< 1  'BEGIN {
  "date" | getline dt
  close("date")
  print "start:", dt
  }
system("sleep 3")
END {
  "date" | getline dt
  close("date")
  print "end:", dt
  }' 
start: Fri Nov  6 15:47:23     2009
end: Fri Nov  6 15:47:27     2009

It works! Thank you very much dude!