-
Notifications
You must be signed in to change notification settings - Fork 0
/
logparser.sh
175 lines (107 loc) · 3.89 KB
/
logparser.sh
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/bash
function mining_usernames
{
str=$(awk 'BEGIN{s="\n"}; {temp="\n" $3 "\n"; if (!match(s,temp)) s=s$3 "\n"}; END{print s}' "$1" | sort)
for user in $str
do
awk -v u="$user" 'BEGIN{count=0}; $3 == u {count++}; END{print count " " u}' "$1"
done
}
function count_browsers
{
awk 'BEGIN{firefox=0;edge=0;chrome=0;safari=0};
{
if (match($0,/.*Mozilla\/.*Firefox\/([0-9]*\.)*[0-9]*/))
firefox++;
else if (match($0,/.*Mozilla\/.*Chrome\/.*Safari\/.*Edg\/([0-9]*\.)*[0-9]*/))
edge++;
else if (match($0,/.*Mozilla\/.*Chrome\/.*Safari\/([0-9]*\.)*[0-9]*/))
chrome++;
else if (match($0,/.*Mozilla\/.*Safari\/([0-9]*\.)*[0-9]*/))
safari++;
};
END{printf "Firefox %s\nChrome %s\nEdge %s\nSafari %s\n", firefox, chrome, edge, safari}' "$1"
}
if [ "$#" -eq 0 ]; then
echo "Hello World"
else
case "$1" in
*.log)
if [ ! -f "$1" ]; then
echo "File $1 does not exist"
exit 1
fi
;;
*)
echo "Wrong File Argument"
exit 1
;;
esac
case "$#" in
1) #One argument ===================================================
sed -n 'p' "$1"
echo ""
;;
*) #More arguments ===================================================
case "$2" in
"--usrid")
case "$#" in
2) mining_usernames "$1" ;;
3) awk -v str="$3" '$3 == str {print $0}' "$1" ;;
*) echo "Too Many Arguments";;
esac
;;
"-method")
case "$#" in
2) echo "Wrong Method Name";;
3)
case "$3" in
"GET" | "POST")
sed -n '/.*\"'"$3"' .*/p' "$1"
;;
*) echo "Wrong Method Name";;
esac
;;
*) echo "Too Many Arguments";;
esac
;;
"--servprot")
case "$#" in
2) echo "Wrong Network Protocol" ;;
3)
case "$3" in
"IPv4") sed -n '/^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/p' "$1";;
"IPv6") sed -n '/^::1/p' "$1";;
*) echo "Wrong Network Protocol";;
esac
;;
*) echo "Too Many Arguments";;
esac
;;
"--browsers")
case "$#" in
2) count_browsers "$1" ;;
*) echo "Too Many Arguments";;
esac
;;
"--datum")
case "$#" in
2) echo "Wrong Date" ;;
3)
case "$3" in
Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec)
sed -n '/.*\[.*'"$3"'.*/p' "$1"
;;
*)
echo "Wrong Date"
;;
esac
;;
*) echo "Too Many Arguments";;
esac
;;
*) echo "Invalid Argument";;
esac
;;
esac
fi