#!/bin/bash
# Change History
# Who When What
# ############### ########## #################################################
# Simon Vollett 20170517 Original Version
################################################################################
function f_usage
{
echo
echo '#################################### USAGE #################################### '
echo '# ScriptName: dsexport_search.sh <ExportFile> <SearchString> '
echo '# Description: Searches an xml datastage export file for a string. '
echo '# Reports jobs containing that string. '
echo '# Parameters: '
echo '# 1) ExportFile: Fully qualified datastage .xml export file '
echo '# 2) SearchString: Quoted search string '
echo '############################################################################### '
echo
}
# Set date and time now
function f_datetime {
DATE_TIME=`date +'%Y-%m-%d %H:%M:%S'`
}
# Log debug information
function f_debug () {
if [ $1 -le $DEBUG_LEVEL -a $DEBUG_LEVEL -ne 0 ]
then
f_datetime
echo "$DATE_TIME: $2" >> $LOGFILE
fi
}
# Exit unsuccessfully
function f_error_exit {
f_datetime
echo "$DATE_TIME: $2. Exiting." | tee -a $LOGFILE
exit $1
}
################################################################################
# Initialise
################################################################################
DEBUG_LEVEL=1 # 0|1|2|3 -> None|Info|Error|All
THIS_SCRIPT=${0}
THIS_SCRIPT_PATH=${0%/*}
THIS_SCRIPT_NAME=${THIS_SCRIPT##*/}
THIS_SCRIPT_NAME=${THIS_SCRIPT_NAME%.sh}
LOGFILE="$THIS_SCRIPT_PATH/$THIS_SCRIPT_NAME"".log"
f_debug 1 "THIS_SCRIPT: "$THIS_SCRIPT
f_debug 1 "THIS_SCRIPT_PATH: "$THIS_SCRIPT_PATH
f_debug 1 "THIS_SCRIPT_NAME: "$THIS_SCRIPT_NAME
f_debug 1 "LOGFILE: "$LOGFILE
if [ $# -ne 2 ] # confirm correct number of args
then
f_usage; f_error_exit 2 "Incorrect number of args"
fi
if [ ! -r "$1" ] # check first arg is a readable file
then
f_usage; f_error_exit 3 "First arg not a readable file"
fi
################################################################################
# Process
################################################################################
grep -ni "<Job Identifier=" $1 > ./dsexport_search.tmp # list of job start lines including job name
grep -noi "</Job>" $1 >> ./dsexport_search.tmp # list of job end lines
grep -noi "$2" $1 >> ./dsexport_search.tmp # list occurences of string in export file
cat ./dsexport_search.tmp | sort -n > dsexport_search.tmp2 # sort combined output by line number
cp /dev/null ./dsexport_search.tmp3 # Initialise
while read -r line;
do
f_debug 3 "line: "$line
LINE_NUM="$( cut -d ':' -f 1 <<< "$line" )"; f_debug 3 "LINE_NUM: "$LINE_NUM
LINE_TYPE="$( cut -d ':' -f 2 <<< "$line" )"; f_debug 3 "LINE_TYPE: "$LINE_TYPE
if [ "$LINE_TYPE" != "</Job>" -a "$LINE_TYPE" != "$2" ]
then
CURRENT_JOB="$( cut -d '"' -f 2 <<< "$LINE_TYPE" )"; f_debug 3 "CURRENT_JOB: "$CURRENT_JOB
fi
if [ "$LINE_TYPE" == "$2" ]
then
echo $CURRENT_JOB >> ./dsexport_search.tmp3
fi
if [ "$LINE_TYPE" == "</Job>" ]
then
CURRENT_JOB=""; f_debug 3 "CURRENT_JOB: "$CURRENT_JOB
fi
done < dsexport_search.tmp2
echo "Jobs containing string: "$2
echo "==========================================================="
cat ./dsexport_search.tmp3 | uniq | sort | sed '/^\s*$/d'
exit 0
Related