-
Notifications
You must be signed in to change notification settings - Fork 0
/
apalognorm
99 lines (91 loc) · 2.77 KB
/
apalognorm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh
######################################################################
#
# APALOGNORM : a normalizer for logs of the Apache combined format
#
# Written by Rich Mikan (richmikan[at]richlab.org) at 2017/01/31
#
# Usage : apalognorm [-s string] <logfile>
# <logfile> should be written with Apache combine format.
# -s Set the substitute string you want to convert from
# the space character. (default is "_")
#
# * This is a filter convert every space character to the other one.
# It is very useful from the various Unix command to treat.
# * e.g. the following command give you the referer field.
# $ apalognorm http-access.log | awk '{print $8}'
#
######################################################################
# definition: print the usage and exit
print_usage_and_exit () {
cat <<-USAGE 1>&2
Usage : ${0##*/} [-s string] <logfile>
<logfile> should be written with Apache combine format.
-s Set the alternative character you want convert from
the space character. (default is "_")
Version : Mon May 5 11:28:22 JST 2014
USAGE
exit 1
}
# initialization
PATH="$(command -p getconf PATH):${PATH:-}"
# parse the arguments
s_opt='_'
file=''
i=0
optmode=''
for arg in "$@"; do
i=$((i+1))
if [ -z "$optmode" ]; then
case "$arg" in
-s*)
ret=$(echo "_${arg#-s}" | sed '1s/^_//')
if [ -n "$ret" ]; then
s_opt=$ret
else
optmode='s'
fi
;;
*)
if [ -z "$file" ]; then
[ $i -eq $# ] || print_usage_and_exit
file=$arg
else
print_usage_and_exit
fi
;;
esac
elif [ "$optmode" = 's' ]; then
s_opt=$arg
optmode=''
else
print_usage_and_exit
fi
done
if [ ! -f "$file" ] &&
[ ! -c "$file" ] &&
[ ! -p "$file" ] &&
[ "_$file" != '_-' ] &&
[ ! -z "$file" ]; then
echo "${0##*/}: No such file found" 1>&2
print_usage_and_exit
elif [ -z "$file" ]; then
file='-'
fi
# convert the substitute string for setting to the sed command
sub=$(printf '%s\n' "$s_opt" |
sed 's/\([\&/]\)/\\\1/g' )
# Define some marks for converting
RS=$(printf '\036') # a mark for the real new lines
LF=$(printf '\\\n_');LF=${LF%_} # LF for the sed command
# Convert the file
if [ "_$file" = '_-' ]; then #
sed 's/^\(.*\)$/\1'"$RS"'/' 2>/dev/null #
else #
sed 's/^\(.*\)$/\1'"$RS"'/' "$file" 2>/dev/null #
fi |
sed 's/"\([^"]*\)"/'"$LF"'"\1"'"$LF"'/g' |
sed 's/\[\([^]]*\)\]/'"$LF"'[\1]'"$LF"'/g' |
sed '/^["[]/s/[[:blank:]]/'"$sub"'/g' |
tr -d '\n' |
tr "$RS" '\n'