Just wondering why this is not working?
this is my awk code, converting “hh:mm:ss” format to seconds
a.awk
3 BEGIN {
4 FS=":";
5 }
6
7 {
8 retval = 0;
9 for (i = 1; i <= NF; i++) {
10 retval += $i * 60 ** (NF-i);
11 }
12 print $retval;
13 }
14
input.txt
59:22:40
$ cat input.txt | awk -f a.awk
//<empty>
$
however, I try it on command line:
$ echo "00:59:30" | awk 'BEGIN { FS=":" } { retval = 0; for (i = 1; i <= NF; i++) { retval += $i * 60 ** (NF-i); } print retval;}'
3570
what’s wrong with a.awk ?
update just for clarifcation
$ awk --version
GNU Awk 4.0.1
Copyright (C) 1989, 1991-2012 Free Software Foundation.
That
forloop is cute, but this seems more direct and easier to understand.I think the problem with your loop is in the exponentiation. My version, at least, doesn’t support any
**operator. This might work better for you. Also, be careful with your dollar signs. You need them for fields; you don’t need them for variables.