Created
September 28, 2017 15:45
-
-
Save wytten/9ef322ac9a0a6289b15305880a3f5115 to your computer and use it in GitHub Desktop.
Use awk to count log events by day, or by hour assuming each line starts with (date)(space)(hour)(colon)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# EXAMPLE 1, BY DAY (default delimiter space) | |
# otus.decare.com:/logs/foster/WebLogic/DDS_Prod/claimservices > ~/bin/count-log-events.sh " MapMessage received" *.out | |
# 2016-08-11 35306 | |
# | |
# EXAMPLE 2, BY HOUR (delimiter colon) | |
# otus.decare.com:/logs/foster/WebLogic/DDS_Prod/claimservices > ~/bin/count-log-events.sh -F: " MapMessage received" *.out | |
# 2016-08-11 02 6501 | |
# 2016-08-11 03 3529 | |
# 2016-08-11 04 13221 | |
# 2016-08-11 05 3344 | |
# 2016-08-11 06 1307 | |
# 2016-08-11 07 5941 | |
# 2016-08-11 08 1465 | |
# otus.decare.com:/logs/foster/WebLogic/DDS_Prod/claimservices > | |
USAGE="Usage: $0 [-F delim] pattern files..." | |
while getopts "F:" opt; do | |
case $opt in | |
F ) delim="-F$OPTARG" | |
shift;; | |
* ) echo $USAGE | |
exit 1 | |
esac | |
done | |
if [ $# -eq 0 ]; then | |
echo $USAGE | |
exit 1 | |
fi | |
pattern=$1 | |
shift | |
awk $delim -v pattern="$pattern" '$0 ~ pattern {counts[$1] = counts[$1] + 1; } END { for (val in counts) print val, counts[val]; }' "$@" | sort |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment