We had to figure out the usage against one of our biggest table to decide how to archive it. We keep a history of all queries issued against the table and I wanted to extract the date filter used in the sql.
There are many ways to do it but I wanted to do it in bash script just for fun...Probably not the most efficient way of doing it but I'm posting this anyway mostly for my own benefit in case I need to figure out how to do something in bash..like how to find the position of a word in a string, how to extract words from a line etc etc
#! /bin/bash
#goal: to extract the dates used in where condition in sql ie adddate >= '2013-11-01' or daystring=20131105
nzsql -t -host serverhere -d databasehere -u usernamehere -W passwordhere -c "select QH_SESSIONID, QH_TSUBMIT, QH_USER, QH_SQL from QUERYHISTORY" > queryhistory.txt 2>&1
rm -f lhqueryhistory1.csv
while read line
do
if [ -z "$line" ];
then
echo "line is empty"
break
fi
sessionid=`echo $line | cut -d"|" -f1`
starttime=`echo $line | cut -d"|" -f2`
username=`echo $line | cut -d"|" -f3`
sql=`echo $line | cut -d"|" -f4`
#cut out sql starting from the where word
wherepos=$(echo $sql | awk '{print match(tolower($0), "where")}')
#if no where condition skip that sql
if [ ${wherepos:-0} -eq 0 ];
then
continue
fi
wheresql=$(echo ${sql:${wherepos}})
#cut out sql starting from the adddate or daystring word
wherepos2=$(echo $wheresql | awk '{print match(tolower($0), "adddate|daystring")}')
if [ ${wherepos2:-0} -eq 0 ];
then
wheresql2="none and"
else
wherepos2=$(($wherepos2 - 1))
wheresql2=$(echo ${wheresql:${wherepos2}})
fi
#cut out sql up until the and, group or limit word
wherepos3=$(echo $wheresql2 | awk '{print match(tolower($0), "and|group|limit")}')
if [ ${wherepos3:-0} -eq 0 ];
then
wheresql3=$wheresql2
else
wherepos3=$(($wherepos3 - 1))
wheresql3=$(echo ${wheresql2:0:${wherepos3:-0}})
fi
#extract the date (before single quote)
wherepos4=$(expr index "$wheresql3" "\'") #look for single quote
wheresql4=$(echo ${wheresql3:${wherepos4}})
if [ ${wherepos4:-0} -eq 0 ];
then
wheresql4=$wheresql3
else
wherepos4=$(expr index "$wheresql3" "\'")#look for next single quote
wherepos4=$(($wherepos4 - 1))
wheresql4=$(echo ${wheresql3:${wherepos4}})
fi
finalrow="$sessionid|$starttime|$username|$wheresql4"
echo $finalrow >> lhqueryhistory1.txt
done < queryhistory.txt
rm -f queryhistory.txt