Skip to content

Instantly share code, notes, and snippets.

@syzdek
Last active September 19, 2018 03:12
Show Gist options
  • Save syzdek/838573086094ebd01049b315ca94ae0c to your computer and use it in GitHub Desktop.
Save syzdek/838573086094ebd01049b315ca94ae0c to your computer and use it in GitHub Desktop.
#!/bin/bash
FILE="${1}"
PROG_NAME="$(basename "${0}")"
if test -z "${FILE}";then
echo "Usage: ${PROG_NAME} <file>"
exit 1
fi
if test ! -f "${FILE}";then
echo "${PROG_NAME}: ${FILE}: file not found"
exit 2
fi
# set initial values
LINECOUNT="$(wc -l "${1}" |awk '{print$1}')"
COUNT=0
SUM=0
LASTPAIR="x"
# start loops
while test ${COUNT} -lt ${LINECOUNT};do
COUNT=$((${COUNT}+1))
LINE="$(sort "${FILE}" |head -${COUNT} |tail -1)"
ARG1="$(echo "${LINE}" |awk '{print$1}')"
ARG2="$(echo "${LINE}" |awk '{print$2}')"
ARG3="$(echo "${LINE}" |awk '{print$3}')"
# compare logs
if test "${LASTPAIR}" == "${ARG1}:${ARG2}";then
SUM=$((${SUM} + ${ARG3}))
else
test "${LASTPAIR}" == "x" || echo -e "${LASTPAIR/:/\t}\t${SUM}"
LASTPAIR="${ARG1}:${ARG2}"
SUM=${ARG3}
fi
done
echo -e "${LASTPAIR/:/\t}\t${SUM}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment