0

I want to take time difference of two time stamp but getting an error while passing timestamp variable from shell toawk.

Shell code:

FTIMESTAMP="2015-07-01 12:30:50"
gawk -v FTIMESTAMP=$FTIMESTAMP -v DSECONDS=$DSECONDS -f test.awk /home/abc/TShift.csv 

Error is:

gawk: 12:30:50
gawk:   ^ syntax error

Escaping character is also not working FTIMESTAMP="2015-07-01 12\:30\:50".

I have another timestamp in awk and want to take time difference between them.

cuonglm
  • 150,973
  • 38
  • 327
  • 406
Aashu
  • 741
  • 4
  • 15
  • 24
  • See how that's one of the examples at [Security implications of forgetting to quote a variable in bash/POSIX shells](http://unix.stackexchange.com/q/171346) – Stéphane Chazelas Oct 09 '15 at 06:58

2 Answers2

2

You need to quote variables to prevent shell from performing split+glob:

gawk -v FTIMESTAMP="$FTIMESTAMP" -v DSECONDS="$DSECONDS" ...

A note that -v var="$shell_var" will expand escape sequences in $shell_var. You need to use ENVIRON or ARGV variables to pass $shell_var as-is from shell to awk.

cuonglm
  • 150,973
  • 38
  • 327
  • 406
0

try either

gawk -v FTIMESTAMP="$FTIMESTAMP" -v DSECONDS=$DSECONDS -f test.awk /home/abc/TShift.csv 

there is a white in $FTIMESTAMP

or in calling shell

FTIMESTAMP="2015-07-01 12:30:50"
export FTIMESTAMP

and in awk's script (in BEGIN { .. } section)

FTIMESTAMP=ENVIRON["FTIMESTAMP"] ;
Archemar
  • 31,183
  • 18
  • 69
  • 104