The following awk code is working as expected.
I want to check if the second field $2 is 0 and use “setex” command instead of default “hincrby”.
BEGIN {
# all fields are separated by ^
FS = "^";
}
{
# $7 is the date and time in the form yyyy-mm-dd hh:mm:ss.
# Split at colons to get hours minutes and seconds into a[1]
# through a[3]. Round minutes to nearest 5.
split($7, a, ":");
split(gensub(/-/,"","g",$7),b,"~");
a[2] = int(a[2]);
printf "hincrby r:%s:%s %s:%02d:00 1\\r\\n\n zadd RequestSet %s r:%s:%s\\r\\n\n ", $1, $2, a[1], a[2], b[1], $1, $2;
}
The above code will output like this…
hincrby r:565:14718 2012-03-10~12:55:00 1\r\n zadd RequestSet 20120310 r:565:14718\r\n
If the creativeid is 0 then the following output is expected:
hincrby r:565:0 2012-03-10~12:55:00 1\r\n zadd RequestSet 20120310 r:565:14718\r\n hincrby r:565:14718 nods 1\r\n
For all other creativeids ($2), I need another statement generated with complete date ($7)
hincrby r:565:14718 2012-03-10~12:55:00 1\r\n zadd RequestSet 20120310 r:565:14718\r\n setex xyzabc:r 172800 2012-03-10~12:59:49\r\n
In other words, I am trying to write the following PHP logic to awk
if($creativeid !=0 ){
$pipe->setex($cb.':r','172800',$datetime);
}else{
$pipe->hincrby("r:".$zone.":".$creativeid,'nods',1);
}
Update:
The following if-then-else code does not seem to work:
if $2 = 0
printf "hincrby r:%s:%s %s:%02d:00 1\\r\\n\n zadd RequestSet %s r:%s:%s\\r\\n\n hincrby r:%s:%s nods 1\r\n", $1, $2, a[1], a[2], b[1], $1, $2, $1, $2;
else
printf "hincrby r:%s:%s %s:%02d:00 1\\r\\n\n zadd RequestSet %s r:%s:%s\\r\\n\n setex %s:r 172800 %s", $1, $2, a[1], a[2], b[1], $1, $2, $5, $7;
a[2]=0will always be true. it is just an assignment…a[2]="anything"will be true unless it it an invalid value.Note that
if( a[2]=0 )does change the value ofa[2]…