Results 1 to 6 of 6
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.
- 11-26-2012 #1Just Joined!
- Join Date
- Nov 2012
- Posts
- 4
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.
- 11-26-2012 #2Linux Newbie
- Join Date
- Nov 2012
- Posts
- 136
hi,
read column into an arrayCode: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
- 11-27-2012 #3Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,202
Hi.
Similarly:
producing: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
Best wishes ... cheers, drlCode:% ./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
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 )
- 11-27-2012 #4Linux Newbie
- Join Date
- Nov 2012
- Posts
- 136
yes, a bit more straight:
oops, I missed the two digits partCode: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
- 11-27-2012 #5Linux Newbie
- Join Date
- Dec 2011
- Posts
- 139
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.
- 11-28-2012 #6Just Joined!
- Join Date
- Nov 2012
- Posts
- 4
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


Reply With Quote
