From d3e52a72b19b64f3443204fe9cac44637217f30b Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Sat, 25 Mar 2023 15:11:38 -0700 Subject: less repetition in awk filter --- backomp | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/backomp b/backomp index 1517ef5..37efaf5 100755 --- a/backomp +++ b/backomp @@ -2,37 +2,33 @@ set -e -# FIXME: clean up repetition ! read -rd '' FILTER_AWK <<'EOF' function flr(n, s) { return int(n / s) * s } BEGIN { - if (match(retain, /([*0-9]+)h/, md)) hr = md[1] - if (match(retain, /([*0-9]+)d/, md)) dr = md[1] - if (match(retain, /([*0-9]+)w/, md)) wr = md[1] - if (match(retain, /([*0-9]+)m/, md)) mr = md[1] + split("h d w m", buckets) + for (i in buckets) + if (match(retain, "([*0-9]+)" buckets[i], md) && md[1]) + ret[buckets[i]] = md[1] } { t = mktime(gensub(/(.*\/|^)(.+)-(..)-(..)-(..)(..)(..)/, "\\2 \\3 \\4 \\5 \\6 \\7", 1), 1) - h = flr(t, 60*60) - d = flr(t, 24*60*60) - m = mktime(strftime("%Y %m", t, 1) " 01 00 00 00", 1) - w = m + flr(t - m, 7*24*60*60) -} + bt["h"] = flr(t, 60*60) + bt["d"] = flr(t, 24*60*60) + bt["m"] = mktime(strftime("%Y %m", t, 1) " 01 00 00 00", 1) + bt["w"] = m + flr(t - m, 7*24*60*60) -h in hkeep || hr == "*" || hc++ < hr { hkeep[h] = $1 } -d in dkeep || dr == "*" || dc++ < dr { dkeep[d] = $1 } -w in wkeep || wr == "*" || wc++ < wr { wkeep[w] = $1 } -m in mkeep || mr == "*" || mc++ < mr { mkeep[m] = $1 } + for (b in ret) + if ((b, bt[b]) in bkeep || ret[b] == "*" || bc[b]++ < ret[b]) + bkeep[b, bt[b]] = $1 +} END { - for (i in hkeep) keep[hkeep[i]]++ - for (i in dkeep) keep[dkeep[i]]++ - for (i in wkeep) keep[wkeep[i]]++ - for (i in mkeep) keep[mkeep[i]]++ + for (i in bkeep) + ++keep[bkeep[i]] asorti(keep) for (i in keep) print keep[i] -- cgit v1.2.3-70-g09d2