Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7671459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:03:45+00:00 2026-05-31T16:03:45+00:00

The following awk code is working as expected. I want to check if the

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:03:47+00:00Added an answer on May 31, 2026 at 4:03 pm
    cmd = (a[2] == 0) ? "hincrby" : "setx"
    print cmd    
    
    # or 
    
    if( a[2] == 0 ) print "hincrby"
    else print "setx"   
    

    a[2]=0 will 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 of a[2] …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following awk code is working as expected. The date like 2012-03-10~12:59:41 is rounded
I have the following code that uses 'paste' and AWK script inside Perl. use
I have found the following command in AWK useful in Vim :'<,'>!awk '{ print
Given the following command, echo "1: " | awk '/1/ -F ":" {print $1}'
I want to use the system command in a bash shell script. Being more
I have output with this format: /ignore-this/^/../I/want/this@ignore-this I am trying to use an awk
I'm trying to use the following awk script to count every 1000 records from
The following command prints the correct mysql commands. But I want to execute them
The following command works as expected and shows the number of databases in the
I'm using the following awk command in an expect script to get the gateway

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.