dot

packages and services management
Log | Files | Refs | README

commit f1dcb8abc192d5a6d095139c516f7cf048542607
parent a46405bd3adce64dfd26347f74bd92a39bc40711
Author: Josuah Demangeon <mail@josuah.net>
Date:   Sun, 11 Mar 2018 18:52:11 +0100

converted sort to use awk

Diffstat:
Abin/.agenda.swp | 0
Mbin/agenda | 56+++++++++++++++++++++++++++++++++-----------------------
Dbin/sort.awk | 53-----------------------------------------------------
3 files changed, 33 insertions(+), 76 deletions(-)

diff --git a/bin/.agenda.swp b/bin/.agenda.swp Binary files differ. diff --git a/bin/agenda b/bin/agenda @@ -71,20 +71,24 @@ function swap(array, a, b) function sort(array, beg, end) { - if (end - beg < 2) + if (beg >= end) # end recursion return; - a = beg; + + a = beg + 1; # #1 is the pivot b = end; while (a < b) { - while (a < b && array[a] < array[beg]) + while (a < b && array[a] <= array[beg]) # beg: skip lesser a++; - while (a < b && array[a] > array[beg]) + while (a < b && array[b] > array[beg]) # end: skip greater b--; - swap(array, a, b); + swap(array, a, b); # found 2 misplaced } - swap(array, beg, a); - sort(array, beg, a); - sort(array, a, end); + + if (array[beg] > array[a]) # put the pivot back + swap(array, beg, a); + + sort(array, beg, a - 1); # sort lower half + sort(array, a, end); # sort higher half }' import="$awk"' @@ -119,32 +123,40 @@ BEGIN { event["DESCRIPTION"]); }' -sort=' +sort="$awk"' #SORT [BESCL] BEGIN { - sort = "sort | cut -f " length(SORT) + 2 "-"; + FS = ":"; + sortcmd = "cut -f " length(SORT) + 2 "-"; } -/^B:/ { - nb = 1; +fn == "" && /^B:/ { + prev = nb + 1; line["B"] = line["E"] = line["S"] = ""; line["C"] = line["L"] = line["D"] = ""; } -/^[BESCLD]:/ { - line[nb++] = sprintf("%08d\t%s", NR, $0); +fn == "" && /^[BESCLD]:/ { + line[++nb] = sprintf("%08d\t%s", NR, $0); line[$1] = substr($0, 3); } -/^:$/ { - line[nb++] = sprintf("%08d\t%s", NR, $0); +fn == "" && /^:$/ { prefix = ""; - + line[++nb] = sprintf("%08d\t:", NR); for (i = length(SORT); i > 0; i--) prefix = line[substr(SORT, i, 1)] "\t" prefix; - for (i = 1; i < nb; i++) - printf("%s%s\n", prefix, line[i]) | sort; + for (i = prev; i <= nb; i++) + line[i] = sprintf("%s%s", prefix, line[i]); +} + +END { + sort(line, 1, nb); + for (i = 1; i < nb; i++) { + sub("([^\t]*\t){" length(SORT) + 1 "}", "", line[i]); + printf("%s\n", line[i]) + } }' view="$awk"' @@ -230,8 +242,7 @@ case "$1" in (all) cd "$AGENDA" awk -v SORT="${2:-BECLS}" "$sort" * | - awk -v OFF="$(date +%z | cut -c 1-3)" "$view" | - less + awk -v OFF="$(date +%z | cut -c 1-3)" "$view" ;; ('') find "$AGENDA" ! -type d ! -name '*.txt' -exec awk "$list" {} \; | sort @@ -243,7 +254,6 @@ usage: agenda -a <name> YYYY/MM/DD HH:MM [YYYY/MM/DD] HH:MM agenda [<name> [BESCL]] EOF awk -v SORT="${2:-BECLS}" "$sort" "$AGENDA/$1" | - awk -v OFF="$(date +%z | cut -c 1-3)" "$view" | - less + awk -v OFF="$(date +%z | cut -c 1-3)" "$view" ;; esac diff --git a/bin/sort.awk b/bin/sort.awk @@ -1,53 +0,0 @@ -#!/usr/bin/awk -f - -function swap(array, a, b) -{ - tmp = array[a]; - array[a] = array[b]; - array[b] = tmp; -} - -function debug(array, beg, end) -{ - printf("[ "); - for (i = beg; i <= end - 1; i++) - printf("%s, ", array[i]); - printf("%s ]\n", array[i]); -} - -function sort(array, beg, end) -{ - if (beg >= end) # end recursion - return; - - debug(array, beg, end); - - a = beg + 1; # #1 is the pivot - b = end; - while (a < b) { - while (a < b && array[a] <= array[beg]) # beg: skip lesser - a++; - while (a < b && array[b] > array[beg]) # end: skip greater - b--; - swap(array, a, b); # found 2 misplaced - } - if (array[beg] > array[a]) - swap(array, beg, a); # put the pivot back - - debug(array, beg, end); - print(""); - - sort(array, beg, a - 1); # sort lower half - sort(array, a, end); # sort higher half -} - -{ - array[NR] = $0 -} - -END { - sort(array, 1, NR); - for (i = 1; i <= NR; i++) - printf(" %s", array[i]); - print(""); -}