Hi,
is possible in bash script change numbers "01140" to "0 hours, 11 minutes, 40 seconds" ?
Thanks!
Hi,
is possible in bash script change numbers "01140" to "0 hours, 11 minutes, 40 seconds" ?
Thanks!
Yes, and depending of your knowledge of UNIX many approaches and solutions...
What have you tried?
I tried:
echo "01140" | rev | cut -c 5- | rev
echo "01140" | rev | cut -c 3- | rev|cut -c2-
echo "01140" | rev | cut -c 1- | rev|cut -c4-
I need a command on a single line
I need the result as: 0:11:40 (11 minutes and 40 seconds)
thanks!
number=01140
numbers=$(printf "%6s" $number)
numbers=${numbers// /0}
echo ${numbers:0:2} hours, ${numbers:2:2} minutes, ${numbers:4:2} seconds
caveat for more than 99 hours.
A quick note to add:
numbers=$(printf "%6s" $number)
numbers=${numbers// /0}
could also be combined as:
numbers=$(printf "%06s" "$number")
true that. but I wanted to keep two digits echo for each column. In case of a number like: 140
note: my bash version thinks that the number 01140 is not a integer type because of the leading zero. Other shells I use ignore the leading zero and use it as integer. the example solution posted seems to work for the shells I use.
Ah I see OK
By the way I suggested " %6s
" rather than " %6d
" to avoid the octal thing...
I tried both previously but the bash shell still converts the number. If the leading zero is removed then it is treated as integer. Again, only the version of bash I use is doing it.
%06s
is obviously not portable.
On different OS I get different results:
printf "%06s\n" 01140
01140
printf "%06s\n" 01140
001140
However, never this is seen as a number.
--- Post updated at 15:00 ---
Here is another one for bash:
x=01140
x=00$x; echo "${x: -6:2} hours, ${x: -4:2} minutes, ${x: -2} seconds"
--- Post updated at 15:33 ---
The following seems to be pretty portable:
printf "%06.f\n" 01140
001140
printf "%06g\n" "$x"
001140