awk problem - erroring out - unexpected token

can anyone help identify where the issue is here?

awk 'BEGIN { c="perl -e 'print scalar(localtime("'${EPOCHTIME}'")), "\n"'"; c|getline; close( c ); print $2" "$3" "$4" "$6; }'

bash: syntax error near unexpected token `('

can't seem to figure it out. i tried this:

awk 'BEGIN { c='perl -e 'print scalar(localtime("'${REFEPOCH}'")), "\n"''; c|getline; close( c ); print $2" "$3" "$4" "$6; }'

awk: line 2: missing } near end of file

linux,sunsolaris,hpux,aix

The problem is with how you're using single quotes in the shell. The first blank after the second quote ends the awk script. A quick search for how to correctly escape single quotes in the shell should help you resolve the issue.

Regards,
Alister

i have already tried searching online which is why i'm posting it here.

---------- Post updated at 06:22 PM ---------- Previous update was at 02:49 PM ----------

does anyone know how this can be fixed?

i've practually tried everything i can think of:

369  awk 'BEGIN { c="perl -e 'print scalar(localtime("1399663974")), "\n"'" ; c|getline; close( c ); print $2" "$3" "$4" "$6; }'
  370  awk 'BEGIN { c="perl -e \'print scalar(localtime("1399663974")), "\n"'" ; c|getline; close( c ); print $2" "$3" "$4" "$6; }'
  371  awk 'BEGIN { c="perl -e \'print scalar(localtime("1399663974")), "\n"\'" ; c|getline; close( c ); print $2" "$3" "$4" "$6; }'
  372  awk 'BEGIN { c="perl -e \\'print scalar(localtime("1399663974")), "\n"\\'" ; c|getline; close( c ); print $2" "$3" "$4" "$6; }'
  373  awk 'BEGIN { c="perl -e print scalar(localtime("1399663974")), "\n"" ; c|getline; close( c ); print $2" "$3" "$4" "$6; }'
  374  awk 'BEGIN { c="perl -e print scalar(localtime("1399663974")), "\\n"" ; c|getline; close( c ); print $2" "$3" "$4" "$6; }'
  375  awk 'BEGIN { c="perl -e print scalar(localtime("1399663974"))," ; c|getline; close( c ); print $2" "$3" "$4" "$6; }'
  376  awk 'BEGIN { c="perl -e print scalar(localtime("1399663974"))" ; c|getline; close( c ); print $2" "$3" "$4" "$6; }'
  377  awk 'BEGIN { c="perl -e 'print scalar(localtime("1399663974"))'" ; c|getline; close( c ); print $2" "$3" "$4" "$6; }'
  378  awk 'BEGIN { c=/"perl -e 'print scalar(localtime("1399663974"))'"/ ; c|getline; close( c ); print $2" "$3" "$4" "$6; }'
  379  awk 'BEGIN { c="perl -e 'print scalar(localtime("1399663974"))'" ; c|getline; close( c ); print $2" "$3" "$4" "$6; }'
  380  awk 'BEGIN { c="perl -e $'print scalar(localtime("1399663974"))'" ; c|getline; close( c ); print $2" "$3" "$4" "$6; }'
  381  awk 'BEGIN { c="perl -e 'print scalar(localtime("1399663974"))'" ; c|getline; close( c ); print $2" "$3" "$4" "$6; }'
  382  awk 'BEGIN { c="perl -e "'"print scalar(localtime("1399663974"))"'"" ; c|getline; close( c ); print $2" "$3" "$4" "$6; }'

Instead of trying to execute the perl program inside awk you can simply pipe the output to awk:

perl ... | awk '{ print $2" "$3" "$4" "$6; }'

OR in gawk, use strftime function:

awk 'BEGIN {print strftime("%b %d %H:%M:%S", "1399663974")}'
1 Like

two examples of how it could be done with your setup:

awk 'BEGIN{c="perl -le '\''print scalar(localtime(\"1399663974\"))'\''"; c|getline; close(c); print $2,$3,$4,$6}'

or:

awk -v c="perl -le 'print scalar(localtime(\"1399663974\"))'" 'BEGIN{c|getline; close(c); print $2,$3,$4,$6}'
1 Like

thanks so much guys. you just saved me hours of pain.