config

OpenBSD system configuration
git clone git://jacobedwards.org/config
Log | Files | Refs | README

commit 07570ac9cb0af4a8fa0005675880ff9526a6afb9
parent 117263db38522f006f4966fc9504d21b56a7bb61
Author: Jacob R. Edwards <n/a>
Date:   Sat, 20 Nov 2021 18:22:34 -0800

Support any standard wc(1) output in the wcp script

Diffstat:
Mlocal/bin/bin/wcp | 40+++++++++++++++++++++++++---------------
1 file changed, 25 insertions(+), 15 deletions(-)

diff --git a/local/bin/bin/wcp b/local/bin/bin/wcp @@ -3,25 +3,35 @@ # License: GPLv3 # wcp -- word count percent -wc -- "$@" | awk '{ - ++len; - entries[len,1] = $1 - entries[len,2] = $2 - entries[len,3] = $3 - sub("^ +[0-9]+ +[0-9]+ +[0-9]+ ", "", $0); - entries[len,4] = $0; +wc "$@" | awk 'function die(s) { + print s > "/dev/stderr"; + status = s; + exit 1; } + +{ + for (i = 1; i <= NF - 1 && i <= 3; ++i) + entries[NR,i] = $i + if (match($0, "^ +[0-9]+( +[0-9]+)?( +[0-9]+)? ") <= 0) + die("Invalid record"); + entries[NR,NF] = substr($0, RLENGTH + 1); +} + END { - if (entries[len,4] != "total") { - print("Missing total") > "/dev/stderr"; + if (status) exit 1; + + if (entries[NR,NF] != "total") { + if (NR > 1) + die("Missing total"); + ++NR; + for (i = 1; i < NF; ++i) + entries[NR,i] = entries[1,i]; } - for (i = 1; i < len; ++i) { - for (j = 1; j <= 3; ++j) { - printf("%4.1f ", - (entries[len,j] ? (entries[i,j] / entries[len,j]) : 0) * 100); - } - print(entries[i,4]); + for (i = 1; i < NR; ++i) { + for (j = 1; j < NF; ++j) + printf("%4.1f ", (!entries[NR,j] ? 0 : entries[i,j] / entries[NR,j]) * 100); + print(entries[i,NF]); } }'