maandag 21 april 2014

Reading CSV files in bash script

idrolestartstophostvmnr
1role01nonovmhost2011
2role02nonovmhost2012
3role03yesnovmhost2013
4role04yesnovmhost2014
5role05yesnovmhost1511
6role06yesnovmhost1512
7role07yesnovmhost1513
8role08yesnovmhost1514
9role09yesnovmhost921
10role10yesnovmhost932
11role11noyesvmhost943
12role12noyesvmhost954
# hLookupHeaderIndex1
echo "(hLookupHeaderIndex1 id)    :"$(hLookupHeaderIndex1 id)
echo "(hLookupHeaderIndex1 start) :"$(hLookupHeaderIndex1 start)
echo "(hLookupHeaderIndex1 vmnr)  :"$(hLookupHeaderIndex1 vmnr)
# OUTPUT:
#(hLookupHeaderIndex1 id)    :1
#(hLookupHeaderIndex1 start) :3
#(hLookupHeaderIndex1 vmnr)  :6
# vLookup
echo "(vLookup start yes role)    :"$(vLookup start yes role)
echo "(vLookup stop yes role)     :"$(vLookup stop  yes role)
# OUTPUT:
#(vLookup start yes role)    :role03 role04 role05 role06 role07 role08 role09 role10
#(vLookup stop yes role)     :role11 role12
# sortByTwoColumns
ROLES=$(vLookup start yes role)
echo "(vLookup start yes role)                :"$ROLES
echo "(sortByTwoColumns vmnr host role ROLES) :"$(sortByTwoColumns vmnr host role $ROLES)
echo "(sortByTwoColumns host vmnr role ROLES) :"$(sortByTwoColumns host vmnr role $ROLES)
# OUTPUT:
#(vLookup start yes role)                :role03 role04 role05 role06 role07 role08 role09 role10
#(sortByTwoColumns vmnr host role ROLES) :role05 role09 role06 role10 role07 role03 role08 role04
#(sortByTwoColumns host vmnr role ROLES) :role05 role06 role07 role08 role03 role04 role09 role10
# vOptions
echo "(vOptions start)   :"$(vOptions start)
echo "(vOptions host)    :"$(vOptions host)
# OUTPUT:
#(vOptions start)   :no yes
#(vOptions host)    :vmhost151 vmhost201 vmhost92 vmhost93 vmhost94 vmhost95
#!/bin/sh 
function checkCSVTableExists() {
 if [[ -z $CSV_FILE ]] ; then
  echo "$0 Error: export CSV_FILE=./config/config.template.csv $CSV_FILE"
  exit 5
 fi
 if [[ ! -f $CSV_FILE ]] ; then
  echo "$0 Error:CSV_FILE does not exist with name: $CSV_FILE "
  exit 10
 fi
}
# Controleer of de CSV_FILE is gezet, anders stoppen
checkCSVTableExists
# hLookupHeaderIndex1(name)
#
# lookup the index value, starting with 1 for the first column.
# Arg1 String Name of the column
# return int index of column
# Example :$(hLookupHeaderIndex1 'testsoort') 
function hLookupHeaderIndex1() {
 NAME=$1
 # tolowercase
 HEADER=$(head -n1 ${CSV_FILE})
 HEADER=$(tr [A-Z] [a-z] <<< "$HEADER")
 NAME=$(tr [A-Z] [a-z] <<< "$NAME")
 # Check if name in Header
 if [[ $HEADER != *$NAME* ]] ; then
  echo "$0 Error 1001 hLookupHeaderIndex1 not found:  NAME= $NAME HEADER= $HEADER"
  exit 1001
 fi
 PINDEX=$(echo ";"$HEADER";"|sed "s/;$NAME;.*//g"|tr ';' '\n' |wc -l )
 echo $PINDEX;
 return 0
}
# vLookup(column match column)
# lookup matching rows and return values.
# arg1 String headername
# arg2 String match
# arg3 String return column
# return String values in matching column
# Example :$(vLookup  'role' 'role11' 'host')
function vLookup() {
 ColumnIdx=$(hLookupHeaderIndex1 $1 )
 MATCH=$2
 PINDEX=$(hLookupHeaderIndex1 $3 )

 while read LINE ; do
  VALUE=$(echo $LINE | cut -d';' -f$ColumnIdx )
  if [[ $MATCH == $VALUE ]] ; then 
    echo $LINE | cut -d';' -f$PINDEX
  fi
 done < $CSV_FILE
}
# sortByTwoColumns(column1 column2 column_match match_values ..)
#
# arg1 String sort Column name one
# arg2 String sort Column name two
# arg3 String Column name match value
# arg4-n String match values in sorted order
#
function sortByTwoColumns() {
  Column1=$(hLookupHeaderIndex1 $1);shift
  Column2=$(hLookupHeaderIndex1 $1);shift
  Column3=$(hLookupHeaderIndex1 $1);shift
  AWK='{ print $'$Column1'";"$'$Column2'";"$'$Column3'}' # build AWK statement
  GREP=';'$(echo ""$@""|sed 's/ /$|;/g')'$'              # build GREP filter
  tail -n+2 $CSV_FILE|awk -F';' "$AWK"|grep -E $GREP|sort|cut -d';' -f3
}
# vOptions(column)
# arg1 String column name
# return choicelist from the specified column
function vOptions() {
 PINDEX=$(hLookupHeaderIndex1 "$1")
 # skip first line tail -n +2 (start linenumber)
 LIST=$(tail -n +2 $CSV_FILE| cut -d';' -f${PINDEX}|sort|uniq)
 echo $LIST
}

donderdag 23 januari 2014

Calculate Time in bash

#!/bin/sh
# +0:00 or +00:00 or -0:00 or -00:00
# returns seconds as int
function getSeconds() {
 SIGN=$(echo $1|cut -c1)"1"
 HOUR=$(echo $1|cut -d':' -f1|cut -c2- )
 HOUR=$(echo "${HOUR#0}")
 MIN=$(echo $1|cut -d':' -f2)
 MIN=$(echo "${MIN#0}")
 SECONDS=$(((HOUR * 3600) + (MIN * 60)))
 echo $((SECONDS * SIGN))
}

# seconds as int -10, 0 or 70
# returns +0:00 or +23:00 or -1:00 or -11:00
function formatSecondsToTime() {
 dt=$1
 SIGN="+"
 if [[ $1 -lt 0 ]] ; then
  SIGN=$(echo $1|cut -c1)
  dt=$(echo $1|cut -c2-)
 fi
 dm=$(((dt / 60  ) % 60))
 dh=$(((dt / 3600) % 24))
 VALUE=$(printf '%d:%02d' $dh $dm)
 echo $SIGN$VALUE
}

function addMinutes(){
 SEC1=$(getSeconds $1)
 SEC2=$(getSeconds $2)
 CALC=$((SEC1 + SEC2 ))
 #echo "$1 $2 $SEC1 + $SEC2 = $CALC formatted= $(formatSecondsToTime $CALC)"
 echo $(formatSecondsToTime $CALC)
}
function printTime() {
 SECONDS=$(getSeconds $1)
 echo "$1 $SECONDS $(formatSecondsToTime $SECONDS)"
 #echo "$1 $SECONDS $(formatSecondsToTime $SECONDS) $(getSign $1) $(getHours $1) $(getMinutes $1)"
}

# test
printTime "+00:01"
printTime "-00:10"
printTime "+01:00"
printTime "+09:10"
printTime "+09:01"
printTime "-11:10"
printTime "-09:59"
printTime "-23:59"
printTime "-10:59"
printTime "-000:059"
printTime "-25:89"
printTime "-24:61"
printTime "-24:00"
Output:
+00:01 60 +0:01
-00:10 -600 -0:10
+01:00 3600 +1:00
+09:10 33000 +9:10
+09:01 32460 +9:01
-11:10 -40200 -11:10
-09:59 -35940 -9:59
-23:59 -86340 -23:59
-10:59 -39540 -10:59
-000:059 -3540 -0:59
-25:89 -95340 -2:29
-24:61 -90060 -1:01
-24:00 -86400 -0:00
time="+01:22"
echo $(addMinutes $time "+0:01")
echo $(addMinutes $time "-0:01")
echo $(addMinutes $time "+1:01")
echo $(addMinutes $time "-1:01")
echo $(addMinutes $time "+24:01")
echo $(addMinutes $time "-24:01")
Output:
+1:23
+1:21
+2:23
+0:21
+1:23
-22:39