#!/bin/bash
##################################################
# Author: pao
# Date: Jan 6 2009
#
# Script to post-process "user" output into a form suitable for Excel processing
#
#
# Usage
# -----
# $0
#
# e.g.
# $0
#
# Possible Extentions & Limitations:
# ----------------------------------
#
#
# Modification History
# --------------------
#
##################################################
file=$1
#
# Set putput file name
#
opfile=${file}-ordered.csv
if [[ $2 ]]; then # Prepend a Date to filename if provided
dat=$2
opfile=${dat}-${file}-ordered.csv
fi
#Order users part 1: get all different users together in the form of a varible
var=$(uniq -f6 $file | awk 'BEGIN{a=0}{if(NF>6){for(i=7;i<=NF;i=i+2){print $i}}}' | sort | uniq)
#Order users part 2 -- do the ordering
more $file | \
awk -v users="$var" '{n=split(users,a," ");str="";for(i=1;i<=n;i++){start=7;num=8;for(j=1;j<=n;j++){if(match($start,a[i])){b[i]=$num};start+=2;num+=2}};for (i=1;i<=n;i++){if(b[i]>=1){}else{b[i]=0}};st="";for(i=1;i<=n;i++){st=st " " a[i] " " b[i]};print $1,$2,$3,$4,$5,$6,st, "total"}' | \
awk 'BEGIN{start=8;tot=0;l=1}{
if(l==2){tot=0;st=$1;for(i=start;i<=NF;i+=2){st=st " " $i;tot=tot+$i};print st, tot}
if(l==1){st="TIME";for(i=7;i<=NF;i+=2){st=st " " $i};print st;l=2};}' > $opfile
headerInfo=$(head -1 $opfile | awk '{header="SPACE TEXT"; for(i=1;i