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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:48:10+00:00 2026-05-31T09:48:10+00:00

I am looking for awk command to change the time column of a text

  • 0

I am looking for awk command to change the time column of a text file to its nearest 5 minute interval. So 12:56:59 become 12:55:00

The following command works as expected. Thanks for the response.

head r_SERVER_2012-03-10-12-55-00 | awk -F'^' '{print $7}' |  awk '{split($2, a, ":"); printf "%s %s:%02d:00\n", $1, a[1],int(a[2]/5)*5}'

Correct result:

2012-03-10 12:55:00

But I want to show fields other than date as well. The following does not work:

head r_SERVER_2012-03-10-12-55-00 | awk -F'^' '{print $1, $2, $7, $8}' |  awk '{split($2, a, ":"); printf "%s %s:%02d:00\n", $1, a[1],int(a[2]/5)*5}'

Wrong result:

565 14718:00:00

It should be …

565 123 2012-03-10 12:55:00 country
  • 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-31T09:48:11+00:00Added an answer on May 31, 2026 at 9:48 am

    You don’t need multiple awk commands, just one, and you can set awk’s FS (field separator) variable in the script, which you can put in a file:

    $ cat foo.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($7, a, ":");
        a[2] = int(a[2] / 5) * 5;
    
        # print first and second field, then rounded time, then 8th field.
        printf "%s %s %s:%02d:00 %s\n", $1, $2, a[1], a[2], $8;
    }
    $ cat input
    565^123^$3^$4^$5^$6^2012-03-10 12:56:59^country
    $ awk -f foo.awk < input
    565 123 2012-03-10 12:55:00 country
    $ 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a command (or way of doing) the following: echo -n
Looking for an awk (or sed) one-liner to remove lines from the output if
I am looking to replace a nasty shell script that uses awk to trim
Looking for C# class which wraps calls to do the following: read and write
I have a file looking like this, xxxxxx yyyyyy aaaaaa cccccc bbbbbb eeeeee oooooo
There is some log-file. $> cat ./text Tue, 28 Feb 2012 15:43:20 407a3f8bbf704e41bef1f4c0ac24f310 FAILED
If I get an output from a command that outputs information in the following
I'm looking for an easy way to track time I work on a project
I have the following command to show changes to a subversion repository svn log
I'm looking for a method to cast a string to an int in awk.

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.