-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrgrep
More file actions
executable file
·331 lines (276 loc) · 6.77 KB
/
Copy pathrgrep
File metadata and controls
executable file
·331 lines (276 loc) · 6.77 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env bash
# Time-stamp: <2017-11-10 12:58:29 Friday by ahei>
# @file rgrep.sh
# @version 1.0
# @author ahei
readonly PROGRAM_NAME="rgrep"
readonly PROGRAM_VERSION="1.0.0"
home=`cd $(dirname "$0") && pwd`
version()
{
echo "${PROGRAM_NAME} ${PROGRAM_VERSION}"
exit 1
}
# echo to stderr
echoe()
{
printf "$*\n" 1>&2
}
usage()
{
local code=1
local redirect
if [ $# -gt 0 ]; then
code="$1"
fi
if [ "$code" != 0 ]; then
redirect="1>&2"
fi
eval cat "$redirect" << EOF
usage: ${PROGRAM_NAME} [OPTIONS] (-H <HOST> | <CLUSTER>) <PATTERN> [<FILES>]
Options:
-h <HOUR>
Set which hour file to grep.
-d <DAY>
Set which day file to grep.
-D <DAY>
Search all files about this day.
-m <MONTH>
Set which month file to grep.
-S Do not sort files by date time you specified.
-B <TIME_BEGIN>
Set time begin string on every line.
-E <TIME_END>
Set time end string on every line.
-k FIELD_INDEX
Sort files by which field.
-t SEP
Set field delimiter.
GERP
Environment variable used to specify which grep to use.
-C No color display.
-l Only search current host.
-g GREP_OPTIONS
Set grep's options.
-o REMOTE_OPTIONS
Set remote's options.
-n Do not really execute command, only print command to execute.
-P When output, do not insert host prefix.
-v Output version info.
EOF
exit "$code"
}
# $1: pattern
# $2: month
# $3: day
# $4: hour
getTimeStr()
{
local pattern=$1
local month=$2
local day=$3
local hour=$4
local timeStr="$pattern"
if (( month < 0 )); then
month=$(date -d "$((-month)) months ago" +%m)
month=${month#0}
fi
if (( day < 0 )); then
if [ ! "$month" ]; then
month=$(date -d "$((-day)) days ago" +%m)
month=${month#0}
fi
day=$(date -d "$((-day)) days ago" +%d)
day=${day#0}
fi
if [ "$hour" != '*' ] && (( hour < 0 )); then
hour=$(printf "%1d" $(date -d "$((-hour)) hours ago" +%k))
fi
if [ "$month" ]; then
timeStr=$(sed -r "s/%m/$(printf %02d $month)/g" <<< "$timeStr")
fi
if [ "$day" ]; then
timeStr=$(sed -r "s/%d/$(printf %02d $day)/g" <<< "$timeStr")
timeStr=$(sed -r "s/%e/$(printf %2d $day)/g" <<< "$timeStr")
fi
if [ "$hour" ] ; then
if [ "$hour" != '*' ]; then
timeStr=$(sed -r "s/%H|%I/$(printf %02d $hour)/g" <<< "$timeStr")
timeStr=$(sed -r "s/%k|%l/$(printf %2d $hour)/g" <<< "$timeStr")
else
timeStr=$(sed -r "s/%H|%I/$hour/g" <<< "$timeStr")
timeStr=$(sed -r "s/%k|%l/$hour/g" <<< "$timeStr")
fi
fi
timeStr=$(date +"$timeStr")
echo -n "$timeStr"
}
# $1 cluster
# $2 day
# $3 hour
getFiles()
{
local cluster=$1
local month=$2
local day=$3
local hour=$4
local files
for key in "${!clusterFiles[@]}"; do
if grep -qx "$key" <<< "$cluster"; then
files="${clusterFiles[$key]}"
fi
done
if [ ! "$files" ]; then
echo -n "$files"
return
fi
getTimeStr "$files" "$month" "$day" "$hour"
}
sortFiles()
{
if [ "$noSort" ] || [ "$isExecute" = 0 ]; then
cat
return
fi
cat | awk -F"$fieldDelimiter" -v timeBegin="$timeBegin" -v timeEnd="$timeEnd" -v keyField="$keyField" '{
if (keyField) {results[$ keyField FNR] = $ 0; next; }
prefix = ":[m[K"; # grep maybe have color output
begin = index($ 0, prefix timeBegin);
if (begin == 0) {begin = index($ 0, timeBegin);}
else {timeBegin = prefix timeBegin;}
end = index($ 0, timeEnd);
timeStr = substr($ 0, begin + length(timeBegin), end - begin - length(timeBegin));
if (length(timeStr) < 10) print;
else results[timeStr FNR] = $ 0;
} END {
size = asorti(results, sortedResults);
for (i = 1; i <= size; i++) print results[sortedResults[i]]}'
}
optInd=1
colorOption=--color=always
fieldDelimiter=" "
remoteOptions=-q
options=":h:d:vo:H:CSB:E:D:ng:lm:k:t:P"
[ ! "$NOT_ADJUST_OPTIONS" ] && eval set -- $(getopt -o "$options" -- "$@")
while getopts "$options" OPT; do
case "$OPT" in
H)
hosts="$hosts $OPTARG"
;;
C)
colorOption=
;;
o)
remoteOptions="$OPTARG"
;;
n)
remoteOptions="$remoteOptions -n -V"
isExecute=0
;;
S)
noSort=1
;;
B)
timeBegin="$OPTARG"
;;
E)
timeEnd="$OPTARG"
;;
h)
hour="$OPTARG"
;;
d)
day="$OPTARG"
;;
D)
day="$OPTARG"
hour='*'
;;
m)
month="$OPTARG"
;;
g)
grepOptions="$OPTARG"
;;
l)
searchLocal=1
;;
k)
keyField="$OPTARG"
;;
t)
fieldDelimiter="$OPTARG"
;;
P)
remoteOptions="$remoteOptions -P"
noPrefix=1
;;
v)
version
;;
:)
case "${OPTARG}" in
?)
echoe "Option \`-${OPTARG}' need argument.\n"
usage
esac
;;
?)
echoe "Invalid option \`-${OPTARG}'.\n"
usage
;;
esac
done
shift $((OPTIND - 1))
[ -z "$GREP" ] && GREP=grep
if [ "$keyField" ]; then
if [ ! "$noPrefix" ]; then
if ! ((keyField++)); then
echoe "Invalid key field \`$OPTARG'!"
exit 1
fi
fi
fi
if { [ ! "$timeBegin" ] || [ ! "$timeEnd" ]; } && [ ! "$keyField" ]; then
noSort=1
fi
if [ ! "$hosts" ]; then
cluster=$1
if [ ! "$cluster" ]; then
echoe "Must specify cluster!"
usage
fi
shift
fi
if [ $# -lt 1 ]; then
echoe "Must specify pattern!"
usage
fi
pattern=$1
shift
if [ $# -ge 1 ]; then
files="$@"
else
if [ -z "$cluster" ]; then
echoe "No files specified, must specify cluster!"
usage
fi
declare -A clusterFiles
. /etc/remoterc 2>/dev/null
. ~/.remoterc 2>/dev/null
files=$(getFiles "$cluster" "$month" "$day" "$hour")
if [ ! "$files" ]; then
echoe "No files specified for cluster $cluster!"
usage
fi
fi
command="$GREP $grepOptions $colorOption '$pattern' $files"
if [ "$searchLocal" = 1 ]; then
if [ "$isExecute" = 0 ]; then
echo "$command"
else
eval "$command"
fi
exit
fi
remote $remoteOptions -H "$hosts" -p "$fieldDelimiter" $cluster "$command" | sortFiles