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 7662205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:44:28+00:00 2026-05-31T13:44:28+00:00

The following awk code is working as expected. The date like 2012-03-10~12:59:41 is rounded

  • 0

The following awk code is working as expected.

The date like 2012-03-10~12:59:41 is rounded to nearest five minutes, for e.g. 2012-03-10~12:55:00

How do I change it so that I can get year-month-day like 20120310 (without dash) ?

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, ":");
    a[2] = int(a[2] / 5) * 5;
    # print first, second and forth field, then rounded time.
    printf "set %s:%s:%s %s:%02d:00\\r\\n\n", $1, $2, $4, a[1], a[2];
}
  • 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-31T13:44:30+00:00Added an answer on May 31, 2026 at 1:44 pm

    Change your split line to use gensub():

    $ cat file 
    1^2^3^4^5^6^2012-03-18~22:09:10
    
    awk '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(gensub(/-/,"","g",$7),a,":")
             a[2] = int(a[2] / 5) * 5;
             # print first, second and fourth field, then rounded time.
             printf "set %s:%s:%s %s:%02d:00\\r\\n\n", $1, $2, $4, a[1], a[2];
         }' file
    

    Output:

    set 1:2:4 20120318~22:05:00\r\n
    
    • 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. I want to check if the
I have the following code that uses 'paste' and AWK script inside Perl. use
I used the following awk in order to count all words that appears in
Following is what I am trying to do using awk. Get the line that
I tried the following code unsuccessfully after using ls -1 awk -F '\n' '{
I created the following awk command in order to print only the line that
i would like to convert the data.txt file into the following output using awk
I have found the following command in AWK useful in Vim :'<,'>!awk '{ print
Given the following command, echo "1: " | awk '/1/ -F ":" {print $1}'
Following this question: Good crash reporting library in c# Is there any library like

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.