summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-07-26 21:10:54 -0400
committerJakob Kaivo <jkk@ung.org>2019-07-26 21:10:54 -0400
commit4c16e777b2994a0aa962a9e7bc1bda7e4f8676fd (patch)
tree5f469a5adda9276b864ba15895477d5b1c3420f9
parent1d151651323894437cc7cfb3895f2f7e651c5294 (diff)
implement shuf
-rw-r--r--.gitignore1
-rw-r--r--Makefile2
-rw-r--r--shuf0
-rw-r--r--shuf.sh18
4 files changed, 20 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 0c0912d..0ee44ae 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,6 +9,7 @@ nproc
pinky
printenv
readlink
+shuf
seq
stdbuf
tac
diff --git a/Makefile b/Makefile
index 66a0ca2..4a8a295 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
.POSIX:
PROGRAMS=arch base64 factor hostid mktemp nproc pinky printenv readlink \
- seq stdbuf sync tac uptime users vdir yes
+ seq shuf stdbuf sync tac uptime users vdir yes
all: $(PROGRAMS)
diff --git a/shuf b/shuf
deleted file mode 100644
index e69de29..0000000
--- a/shuf
+++ /dev/null
diff --git a/shuf.sh b/shuf.sh
new file mode 100644
index 0000000..d0fe4fb
--- /dev/null
+++ b/shuf.sh
@@ -0,0 +1,18 @@
+exec awk "$(tail -n +2 $0)" "$@"
+
+{
+ lines[NR] = $0;
+ printed[NR] = 0;
+}
+
+END {
+ nprinted = 0;
+ while (nprinted < NR) {
+ line = int(rand() * NR);
+ if (!printed[line]) {
+ print lines[line];
+ printed[line] = 1;
+ nprinted++;
+ }
+ }
+}