awk-gold-collection

My best software created for POSIX AWK
git clone git://git.luxferre.top/awk-gold-collection.git
Log | Files | Refs | Submodules | README

commit e92f20e471e5fb766cc7a1144439b06c176940f3
parent 0182ea8f52b5cb00ee8b2bc546eb9826ccd166b6
Author: Luxferre <lux@ferre>
Date:   Tue, 12 Mar 2024 09:57:02 +0200

Added imeigen.awk

Diffstat:
MREADME | 2+-
Autils/imeigen.awk | 30++++++++++++++++++++++++++++++
2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/README b/README @@ -31,6 +31,6 @@ Other AWK scripts have the usage described in their comment headers. * utils/textereo.awk: ASCII art stereogram generator * utils/tgl.awk: The Great Library of useful functions missing in POSIX AWK - +* utils/imeigen.awk: Random valid IMEI generator (with optional TAC prefix) --- Luxferre --- diff --git a/utils/imeigen.awk b/utils/imeigen.awk @@ -0,0 +1,30 @@ +# Valid IMEI generator script +# +# Usage: awk -f imeigen.awk [TAC] +# you can pass any amount of digits, if less than 15 then the rest +# is randomized and the checksum is calculated accordingly +# if more, then it's truncated to 14 and the checksum is calculated +# if no TAC is passed, a fully random valid IMEI is generated +# +# Created by Luxferre in 2024, released into public domain + +# Luhn checksum (picoLuhn variation for 14 digits) +function luhn(imeistr, acc, i, l, d) { + acc = 0 + l = length(imeistr) # must be even in this variation + for(i=0;i<l;i++) { + d = int(substr(imeistr, i+1, 1)) # get current digit + acc = (acc + int(d * ((i%2) == 1 ? 2.2 : 1))) % 10 # accumulate + } + return (10 - acc) % 10 # finalize the check digit +} + +BEGIN { + srand() # init PRNG + imei=ARGV[1] + gsub(/[^0-9]/, "", imei) # remove all non-digits + for(i=0;i<14;i++) # append 14 random digits + imei = imei int(rand()*10) + imei = substr(imei, 1, 14) # truncate to first 14 digits + print(imei luhn(imei)) # output complete IMEI with checksum +}