Advertisement
DRVTiny

Generate final protocol for TTStarter

Aug 1st, 2013
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.42 KB | None | 0 0
  1. #!/bin/bash
  2. shopt -s extglob; set +H
  3.  
  4. [[ $1 == '-x' ]] && { shift; set -x; }
  5.  
  6. startTime='results1.txt'
  7. finTime='results2.txt'
  8. startProtocol='peoples.csv'
  9. resultsCSV='protocol.csv'
  10.  
  11. declare -A k2v=([s]='startTime' [f]='finTime' [p]='startProtocol' [r]='resultsCSV')
  12. while getopts 's: f: p: r:' k; do
  13.  [[ $k == '?' ]] && continue
  14.  eval "${k2v[$k]}='$OPTARG'"
  15. done
  16. shift $((OPTIND-1))
  17.  
  18.  
  19. declare -A StartFin
  20. declare -a TIME line
  21. for f in $startTime $finTime; do
  22.   while read -r l; do
  23.    IFS='#' read -a line <<<"$l"
  24.    ID=${line[0]}
  25.    TS=${line[1]}
  26.    IFS=' .' read -a TIME <<<"$TS"
  27.    StartFin[$ID]+=':'$(( $(date -d "1970-01-01 +${TIME[0]} days ${TIME[1]}" +%s)*1000+${TIME[2]##+(0)} ))
  28.   done <$f
  29. done
  30.  
  31. {
  32.   while read -r l; do
  33.    IFS='#' read -a line <<<"$l"
  34.    ID=$(sed -nr 's%^\s*0*([1-9][0-9]*)\s*$%\1%p' <<<"${line[0]}")
  35.    NAME=${line[1]}; NAME=${NAME##+( )}; NAME=${NAME%%+( )}
  36.  
  37.    if [[ ! ${StartFin[$ID]} ]]; then
  38.     RES=99999999
  39.    elif IFS=':' read -a TIME <<<"${StartFin[$ID]}"; [[ ${#TIME[@]} -ne 3 ]]; then
  40.     RES=88888888
  41.    elif ms=$(( TIME[2]-TIME[1] )); (( ms<=0 )); then
  42.     RES=77777777
  43.    else
  44.     RES=$ms
  45.    fi
  46.      
  47.    lsec=$(( ${#RES}-3 ))
  48.    if (( lsec<0 )); then
  49.     SEC="0.$RES"
  50.    else
  51.     SEC="${RES:0:$lsec}.${RES:$lsec}"
  52.    fi
  53.    echo "${RES}$(printf '%03g' $ID)#$NAME#$ID#$SEC"
  54.   done <$startProtocol
  55. } | \
  56.  sort -n | cut -d'#' -f2- | nl -s'#' -n ln | tee $resultsCSV
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement