#!/bin/bash make_group_chapters() { unset index i tmp input=( "$@" ) chapters=() index=1 chapters[$index]=${input[$((index-1))]} for ((i=1;i<${#input[@]};i++)); do tmp="${chapters[$index]##* }" if (( ${input[$i]%.*} > "${tmp%.*}" )); then chapters[$index]="${chapters[$index]} ${input[$i]}" else ((index++)) chapters[$index]="${input[$i]}" fi done for c in ${!chapters[@]}; do chapter_array[x++]="${chapters[c]}" done } function format_seconds() { awk '{ hr=($1/3600); hd=(sprintf("%02d", hr)) mr=((hr-hd)*60); md=(sprintf("%02d", mr)) s=((mr-md)*60); sd=(sprintf("%02d", s)) t=(sprintf("%02d:%02d:%06.3f" ,hd,md,s)); print t}' <<< $1 } make_group_chapters "$@" for index in "${!chapter_array[@]}"; do for number in ${chapter_array[index]}; do new_element="$new_element $(format_seconds $number)" done array[index]="$new_element" unset new_element done for i in ${!array[@]}; do echo "${array[i]}" done