Back (Current repo: scraps)

random scraps and notes that are useful to me
To clone this repository:
git clone https://git.viktor1993.net/scraps.git
Log | Download | Files | Refs

commit 9ba75222b8ca59ab61ed77993a59cde8fc84fae7
parent 38fc04bde742d872275273d16bce5861faae8e70
Author: root <root>
Date:   Mon, 21 Apr 2025 13:55:29 +0200

add script

Diffstat:
Aawk/gen_csv_data.awk | 48++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 48 insertions(+), 0 deletions(-)

diff --git a/awk/gen_csv_data.awk b/awk/gen_csv_data.awk @@ -0,0 +1,48 @@ +#!/bin/awk + +#A small utility I created when I need some quick and dirty SQL data to test out some queries and ideas + +function isNegative() { + return rand(); +} + +function random(digits, pos_only) { + negative = isNegative(); + if ((negative <= 0.5) && (pos_only == 0)) { + return (rand() * (10 ** digits)) * -1; + } else { + return (rand() * (10 ** digits)); + } +} + +function generateNumber(digits, pos_only, decimal, delimiter){ + result = random(digits, pos_only); + if (decimal == 1) { + return int(result)""delimiter; + } else { + return result""delimiter; + } +} + +BEGIN { + if (digits == ""){digits = 3;} + if (rows == ""){rows = 10;} + if (columns == ""){columns = 4;} + if (pos_only == ""){pos_only = 1;} + if (decimal == ""){decimal = 1;} + if (delimiter == ""){delimiter = ","} + DELIMITER_LEN=length(delimiter)+1; + + for(i = 0; i < rows; i++) { + x=""; + for(j = 0; j < columns; j++) { + if (j + 1 >= columns){ + x = x""generateNumber(digits, pos_only, decimal, delimiter)"\n"; + } else { + x = x""generateNumber(digits, pos_only, decimal, delimiter); + } + } + print substr(x, 1, length(x)-DELIMITER_LEN); + + } +}