-
Notifications
You must be signed in to change notification settings - Fork 6
/
filtering.sh
33 lines (26 loc) · 1.05 KB
/
filtering.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
#!/bin/bash
filter_bash_history() {
file="${1:-$HISTFILE}"
backup="$file.before-filtering.bak"
\mv "$file" "$backup"
# trim leading and trailing whitespace
cat "$backup" | sed -r 's/^\s+//; s/\s+$//' |
# check whether commands have timestamps (man bash -> HISTTIMEFORMAT);
# put timestamp and command in one line to make sorting possible.
if grep -E "^#[0-9]{10}$" </dev/stdin >/dev/null; then
# squashes multiline commands
sed '1i\\' | tr '\n' ' ' | sed -r 's/ (#[0-9]{10}) /\n\1 /g' | sed '1d'
else # add placeholder
sed -n '/^#[0-9]*$/!{s/^/#0000000000 /; p}'
fi |
# remove trivial commands
sed -r '/^#[0-9]* .{,6}$/d' |
sed -r '/^#[0-9]* [a-Z0-9 _/.]{,12}$/d' |
# sort by timestamp
sort --stable --key=1,1 |
# deduplicate, keeping last occurrence (https://stackoverflow.com/a/39076527)
tac | awk '!uniq[substr($0, 12)]++' | tac |
# split entries in two lines again, remove placeholder timestamps
sed -r 's/^(#[0-9]*) /\1\n/' | grep -v "^#0000000000$" > "$file"
echo Done. Original history saved to \"$backup\".
}