Help in using date command inside awk

Hi All,


bash-3.2$ autorep -J BOX_NAME% -l0 | grep BOX_NAME| awk -f awkScript.awk

sh: -c: line 0: unexpected EOF while looking for matching `"'
sh: -c: line 1: syntax error: unexpected end of file
        BOX_NAME SU 06/21/2013 03:44:03 06/21/2013 07:46:37 0

#My awkfile 
{               
       "date +%s -d \""$2" "$3"\"" | getline startTime;
       "date +%s -d \""$4" "$5"\"" | getline endTime;
       duration = (endTime - startTime)/60;
printf "\t" "%s %s %s %s %s %s %s\n",$1,$6,$2,$3,$4,$5,duration
}


when I hardcode the values in places of field variables inside date command , that works.

So basically my question is how should I use field variables like $1,$2 etc in date command in awk , I should I use quotes with them.

I tried finding on google but could not get answer .

Thanks.
Spark.

You could use system() for example.

$ echo "+%w" | awk '{system("date " $1)}'
5
$ awk '{ ("date +%s -d " q $1 s $2 s $3 q )| getline dt; print dt }' q='"' s=' ' <<<'1 day ago'
1371735965
$ awk '{ ("date +%s -d " q $1 s $2 s $3 q )| getline dt; print dt }' q='"' s=' ' <<<'10 day ago'
1370958368

Try using the following awk script:

BEGIN {
  s = " "; q = "'"
  cmd = "date +%s -d "
  }
{
  args = q $2 s $3 q
  (cmd args) | getline startTime
  close(cmd args)
  args = q $4 s $5 q
  (cmd args) | getline endTime
  close(cmd args)
  duration = (endTime - startTime)/60
  printf "\t%s %s %s %s %s %s %s\n", $1, $6, $2, $3, $4, $5, duration
}
1 Like

Thanks radoulov .
This is working like a charm.

Spark.