-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilter-sessions-by-time
executable file
·82 lines (59 loc) · 1.55 KB
/
filter-sessions-by-time
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
#!/bin/bash
show_help()
{
cat <<EOF
$ExecName version $Version
Usage:
$ExecName ERA_BEGIN ERA_END
This program filters a list of iRODS sessions for those that overlap with a
given time interval. It reads the sessions from standard in and writes the
filtered sessions to standard out.
Parameters:
ERA_BEGIN: The beginning time of the interval of interest. The time should be
specified in the form yyyy-MM-dd hh:mm:ss.
ERA_END: The ending time of the interval of interest. The time should be
specified in the form yyyy-MM-dd hh:mm:ss.
Example:
$ExecName '2018-01-24 14:08:56' '2018-01-24 14:09:34' < irods.sessions
EOF
}
readonly Version=1
set -o errexit -o nounset -o pipefail
readonly ExecAbsPath=$(readlink --canonicalize "$0")
readonly ExecName=$(basename "$ExecAbsPath")
main()
{
if [[ "$#" -lt 2 ]]
then
show_help >&2
return 1
fi
local eraBegin="$1"
local eraEnd="$2"
filter "$eraBegin" "$eraEnd"
}
filter()
{
awk --assign ERA_BEGIN="$1" --assign ERA_END="$2" --file - <(cat) <<'EOF'
function validate_era(eraName, eraVal) {
if (eraVal !~ /^[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-6][0-9]$/) {
printf "%s has invalid format: '%s'\n", eraName, eraVal > "/dev/stderr";
exit 1;
}
}
BEGIN {
validate_era("ERA_BEGIN", ERA_BEGIN);
validate_era("ERA_END", ERA_END);
RS = "§";
FS = "•";
}
{
beginTime = substr($2, 2, 19);
endTime = substr($NF, 2, 19);
if (beginTime <= ERA_END && endTime >= ERA_BEGIN) {
printf "§%s", $0;
}
}
EOF
}
main "$@"