shithub: sm2

Download patch

ref: 3fefdf7e247a5e869097f51716055a386950d3c5
parent: cb8d0756a245a61662c8bc63dd0c054c7e0dfdb3
author: qwx <[email protected]>
date: Sun Sep 26 20:54:36 EDT 2021

sm2ger: replace stupid shl/sed/grep -v/awk/sed pipeline with a single awk

previous silly tr/sed shit overflows command line length pretty quick too

--- a/bin/sm2ger
+++ b/bin/sm2ger
@@ -1,9 +1,24 @@
 #!/bin/rc -e
 p=$home/p/sm2/ger
-cd $p
-shl <db | sed 20q >>db0
-grep -v `"{awk '{print $5}' db0 | tr '
-' '|' | sed 's/\|$//;s/\|/\| /g;s/^/ /'} db >db00
-mv db00 db
+cd $p || exit
+awk -v 'nword='20 '
+{
+	d[NR] = $0
+}
+END{
+	srand()
+	for(i=1; i<=nword; i++){
+		do
+			x = int(rand()*1e6) % length(d)
+		while(! (x in d))
+		h[i] = d[x]
+		delete d[x]
+	}
+	for(i in h)
+		print h[i] >>"db0"
+	for(i in d)
+		print d[i]
+}' db >/tmp/sm2.$pid
+mv /tmp/sm2.$pid db
 's&m2' db0 >db1
 cp db1 db0