Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I am having some trouble on rounding to nearest minute. I have a file with different values. One of the columns is time. Ex.: 0:0:17 0:1:18 0:2:19 0:19:33 0:26:49 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2012
    Posts
    5

    rounding to nearest minute


    Hi,
    I am having some trouble on rounding to nearest minute.

    I have a file with different values. One of the columns is time.

    Ex.:
    0:0:17
    0:1:18
    0:2:19
    0:19:33
    0:26:49

    What I need to do is rounding up and down depends on the number of seconds (if up to 30 then rounding down, if more then 30 rounding up). So 0:0:17 will become 00:00 and 0:26:49 will become 00:27.

    I mention that I am on a bash shell.

    Thank you.
    R.

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    211
    hi,

    read column into an array
    Code:
     echo "0:0:17
    0:1:18
    0:2:19
    0:19:33
    0:26:49" | while IFS=':' read -a ar; do echo -n "${ar[0]}:" ; (( ${ar[2]} <= 30 )) && echo "${ar[1]}" || echo "$((${ar[1]}+1))"; done
    0:0
    0:1
    0:2
    0:20
    0:27

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,260
    Hi.

    Similarly:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate decisions and arithmetic in bash.
    
    # Utility functions: print-as-echo, print-line-with-visual-space, debug.
    # export PATH="/usr/local/bin:/usr/bin:/bin"
    pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
    db() { : ; }
    C=$HOME/bin/context && [ -f $C ] && $C printf
    
    FILE=${1-data1}
    
    pl " Input data file $FILE:"
    cat $FILE
    
    # 0-29: 30 values; 30-59: 30 values.
    pl " Results:"
    while IFS=":" read -a t
    do
      (( t[1] = ${t[2]}<30 ? ${t[1]} : ${t[1]}+1 ))
      printf "%02i:%02i\n" ${t[0]} ${t[1]}
    done < $FILE
    
    exit 0
    producing:
    Code:
    % ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
    bash GNU bash 3.2.39
    printf - is a shell builtin [bash]
    
    -----
     Input data file data1:
    0:0:17
    0:1:18
    0:2:19
    0:19:33
    0:26:49
    0:41:29
    0:41:30
    
    -----
     Results:
    00:00
    00:01
    00:02
    00:20
    00:27
    00:41
    00:42
    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. #4
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    211
    yes, a bit more straight:
    Code:
    echo "0:0:17
    0:1:18
    0:2:19
    0:19:33
    0:26:49" | while IFS=":" read -a ar
    do
      printf "%.2d:%.2d\n" ${ar[0]} $(( ${ar[2]}<30 ? ${ar[1]} : ${ar[1]}+1 ))
    done
    00:00
    00:01
    00:02
    00:20
    00:27
    oops, I missed the two digits part

  5. #5
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    280

    read -a. Plain awesome...

    You know, I've been into Linux/etal since 1995 (NetBSD on floppies from dial-up anyone?), and bash scripting the whole time, and I still learn magic from the clever people on this forum.

    I had no idea you could even do 'read -a'. I guess the bash manpage is a bit verbose and I find myself grepping instead of actually reading.

    Thank you watael for your arcane knowledge (which seems obvious now), and thanks to ralub for posting this (rather common) issue.

  6. #6
    Just Joined!
    Join Date
    Nov 2012
    Posts
    5
    Hello all,

    Thank you very much for your quick response. I am kind of new to scripting and your posts (and the forum itself) helped allot.

    Thanks,
    Ralu

  7. #7
    Just Joined!
    Join Date
    Nov 2012
    Posts
    5
    Quote Originally Posted by ralub View Post
    Hello all,

    Thank you very much for your quick response. I am kind of new to scripting and your posts (and the forum itself) helped allot.

    Thanks,
    Ralu

    Hello,

    After a long time, I am back t this problem and I've noticed a mistake. Here's the script and the result:

    echo "0:0:17
    0:1:18
    0:2:19
    0:19:33
    0:26:49
    1:27:51
    21:59:29
    23:59:31" |

    while IFS=":"
    read -a ar

    do

    printf "%.2d:%.2d\n" ${ar[0]} $(( ${ar[2]}<30 ? ${ar[1]} : ${ar[1]}+1 ))

    done


    exit 0



    Result:
    00:01
    00:02
    00:20
    00:27
    01:28
    21:59
    23:60


    Thank you for help,
    Raluca

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •