Skip to content

Instantly share code, notes, and snippets.

@jsianes
Last active December 19, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsianes/5874952 to your computer and use it in GitHub Desktop.
Save jsianes/5874952 to your computer and use it in GitHub Desktop.
A bash shell-script that takes several CSV as input and generates an unified and restructured CSV output
#!/bin/bash
#
# Developed by: Javier Sianes - jsianes@gmail.com
#
# Exit codes:
# 0: Correct program execution
# 1: Total hours doesn't match with partial aggregate hours
# 2: General error or program execution interrupted
# 3: Can't match resource as intern or extern
# 4: Error in command line execution
set_intorext() {
CAT=""
if [ "${NAME}" = "employee01" ];then
CAT="Interno"
fi
if [ "${NAME}" = "employee02" ];then
CAT="Interno"
fi
if [ "${NAME}" = "employee03" ];then
CAT="Externo"
fi
if [ "${NAME}" = "employee04" ];then
CAT="Externo"
fi
if [ "${NAME}" = "employee05" ];then
CAT="Externo"
fi
if [ "${NAME}" = "employee06" ];then
CAT="Externo"
fi
if [ "${NAME}" = "employee07" ];then
CAT="Externo"
fi
if [ "${NAME}" = "employee08" ];then
CAT="Externo"
fi
if [ "${NAME}" = "employee09" ];then
CAT="Externo"
fi
if [ "${NAME}" = "employee10" ];then
CAT="Externo"
fi
if [ "${NAME}" = "employee11" ];then
CAT="Interno"
fi
if [ "${CAT}" = "" ];then
echo 3 > ${T_FILE3}
fi
}
clear_tmps() {
if [ -f ${T_FILE3} ];then
EXIT_CODE=`cat ${T_FILE3}`
else
EXIT_CODE=2
fi
rm -f ${T_FILE} ${T_FILE2} ${T_FILE3} >/dev/null 2>/dev/null
exit ${EXIT_CODE}
}
if [ $# -eq 0 ];then
echo "Usage:"
echo "$0 file.csv [file2.csv] ..."
echo ""
exit 4
fi
init_tempfiles() {
T_FILE="/tmp/${RANDOM}${RANDOM}${RANDOM}${RANDOM}${RANDOM}.tmp"
while [ -f ${T_FILE} ];do
T_FILE="/tmp/${RANDOM}${RANDOM}${RANDOM}${RANDOM}${RANDOM}.tmp"
done
T_FILE2="/tmp/${RANDOM}${RANDOM}${RANDOM}${RANDOM}${RANDOM}.tmp"
while [ -f ${T_FILE2} ];do
T_FILE2="/tmp/${RANDOM}${RANDOM}${RANDOM}${RANDOM}${RANDOM}.tmp"
done
T_FILE3="/tmp/${RANDOM}${RANDOM}${RANDOM}${RANDOM}${RANDOM}.tmp"
while [ -f ${T_FILE3} ];do
T_FILE3="/tmp/${RANDOM}${RANDOM}${RANDOM}${RANDOM}${RANDOM}.tmp"
done
}
init_tempfiles
echo 0 > ${T_FILE3}
trap clear_tmps SIGINT SIGQUIT
while [ $# -gt 0 ];do
FILE=$1
if [ -f ${FILE} ];then
dos2unix ${FILE} >/dev/null 2>/dev/null
cat ${FILE}|grep ';;'|tac|cut -d - -f 2|cut -d \ -f 2-|sed 's/([0-9].*%)//g'|awk '{print $1";"$(NF-1)}' FS=';'|sed 's/,/./g' > ${T_FILE}
NAME=`echo ${FILE} | cut -d \. -f 1 | sed 's/_/ /g'`
set_intorext
TOTAL=`cat ${T_FILE} | grep ^Total | cut -d \; -f 2`
echo 0 > ${T_FILE2}
cat ${T_FILE} | grep -v ^Total |
while read LINE;do
IS_PROJECT_LINE=`echo "${LINE}" | grep \/ | wc -l`
if [ ${IS_PROJECT_LINE} -eq 1 ];then
PROJECT=`echo ${LINE} | cut -d \/ -f 1`
CS=`echo ${LINE} | cut -d \/ -f 2 | cut -d \; -f 1 | cut -c 6-`
if [ "${PROJECT}" = "P9990" ];then
PROJECT="x"
fi
else
DESCRIPTION=`echo ${LINE} | cut -d \; -f 1`
VALUE=`echo ${LINE} | cut -d \; -f 2`
CONT=`cat ${T_FILE2}`
CONT=`echo "scale=2;${CONT}+${VALUE}" | bc -l`
echo ${CONT} > ${T_FILE2}
echo "${NAME};${CAT};${CS};${DESCRIPTION};${VALUE};${PROJECT}" | sed 's/\./,/g'
fi
done
CONT=`cat ${T_FILE2}`
NOT_EQUAL=`echo "${TOTAL} != ${CONT}" | bc -l`
if [ ${NOT_EQUAL} -eq 1 ];then
echo 1 > ${T_FILE3}
fi
fi
shift
done
clear_tmps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment