-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup.sh
83 lines (65 loc) · 1.61 KB
/
cleanup.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
# © 2015–2016 Sebastián González
# http://www.apache.org/licenses/LICENSE-2.0
set -o nounset
#---[ Private functions ]---
-patch-day-field () {
# Replace the separator of date and time with a dash.
while read line; do
echo ${line:0:10}-${line:11}
done
}
-restore-day-field () {
# Replace the separator of date and time with a space.
while read line; do
echo ${line:0:10} ${line:11}
done
}
-filter-date() {
(( 1 <= $# && $# <= 2)) || die "Usage: filter-date <key1> [key2]"
sort --unique --key=$1${2:+,$2} --field=- --stable
}
-without-repetitions() {
sort --reverse | uniq --unique
}
#---[ Vocabulary ]---
last-for-day() {
# Help `sort` find the third field (the day).
# (could be avoided if `sort` supported multiple field separators)
-patch-day-field | -filter-date 1 3 | -restore-day-field
}
last-for-month() {
-filter-date 1 2
}
last-for-year() {
-filter-date 1 1
}
now=$(now)
this_day=${now%% *}
this_month=${this_day%-*}
this_year=${this_month%-*}
this-day() {
egrep "^$this_day.*"
}
this-month() {
egrep "^$this_month.*"
}
this-year() {
egrep "^$this_year.*"
}
today() {
this-day
}
last-3-years() {
egrep "^($this_year|$((this_year-1))|$((this_year-2))).*"
}
# Prints input lines that are not filtered out.
not() {
(( $# == 1)) || die "Usage: not <filter>"
# Duplicate undesired entries and eliminate them.
tee >($1) | -without-repetitions
}
neither() {
(( $# == 2)) || die "Usage: neither <filter1> <filter2>"
# Replicate the entries that should be eliminated.
tee >($1) >($2) | -without-repetitions
}