Skip to content

Instantly share code, notes, and snippets.

@fabiolimace
fabiolimace / stdquote.js
Created March 27, 2025 10:22 — forked from thanpolas/stdquote.js
Normalize all UTF quotes in Javascript
/**
* Will normalize quotes in a given string. There are many variations of quotes
* in the unicode character set, this function attempts to convert any variation
* of quote to the standard Quotation Mark - U+0022 Standard Universal: "
*
* @param {string} str The string to normalize
* @return {string} Normalized string.
* @see https://unicode-table.com/en/sets/quotation-marks/
*/
helpers.stdQuote = (str) => {
@fabiolimace
fabiolimace / cookie-jar-search-by-key.awk
Last active April 5, 2025 22:10
Search records in a Cookie-Jar format file. Moved to https://github.com/fabiolimace/awk-tools
@fabiolimace
fabiolimace / Awk script.awk
Last active April 5, 2025 23:39
Modelos/Templates do Ubuntu
#!/usr/bin/mawk -f
#
# description
#
BEGIN {
}
{
@fabiolimace
fabiolimace / csv-to-tsv.awk
Last active April 5, 2025 21:23
Convert a comma-setarated values file (CSV) into a tab-separated values file (TSV) using AWK, and vice-versa. Moved to https://github.com/fabiolimace/awk-tools
#!/usr/bin/awk -f
#
# Reads a CSV file and prints a TSV file to standard output.
#
# Usage:
# ./csv-to-tsv.awk input.csv > output.tsv
# ./csv-to-tsv.awk -v SEP=";" input.csv > output.tsv
# ./csv-to-tsv.awk -v COLUMNS=10 input.csv > output.tsv
#
@fabiolimace
fabiolimace / zless
Created February 14, 2025 18:54
Gzip tools like zcat
#!/bin/sh
zcat "$1" | less
@fabiolimace
fabiolimace / parse-wget-unicode.awk
Created February 2, 2025 16:02
Transform UTF-16 escape sequences generated by WGET into HTML entities
#!/bin/local/gawk -f
# parse utf-16 escape sequences generated by WGET
# transform them all into HTML entities: `�`
function parse_unicode() {
while (match($0, /\\u[0-9a-f][0-9a-f][0-9a-f][0-9a-f]/)) {
hex = substr($0, RSTART+2, RLENGTH-2);
gsub( "\\\\u" hex, "\\&#x" hex ";", $0);
}
@fabiolimace
fabiolimace / convert-html-entities.sh
Last active February 3, 2025 04:38
Convert HTML entities using AWK
# hexadecimal reference to decimal
echo "--ƒ--" \
| awk '{ while (match($0, /&#x[0-9a-fA-F]+;/)) { n = "0x" substr($0, RSTART + 3, RLENGTH - 4 ); x = sprintf("&#%d;", strtonum(n)); $0 = substr($0, 1, RSTART - 1) x substr($0, RSTART + RLENGTH); }; print }'
--ƒ--
# decimal reference to character
echo "--ƒ--" \
| awk '{ while (match($0, /&#[0-9]+;/)) { n = substr($0, RSTART + 2, RLENGTH - 3 ); sub(/^0+/, "", n); x = sprintf("%c", strtonum(n)); $0 = substr($0, 1, RSTART - 1) x substr($0, RSTART + RLENGTH); }; print }'
--ƒ--
function join_numbers( i, n, x) {
while (match($0, /[ ][0-9,.()-]+[ ][0-9,.()-]+[ ]?/)) {
i = RSTART;
n = RLENGTH;
x = substr($0, i, n);
gsub(" ", "", x);
$0 = substr($0, 0, i-1) " " x " " substr($0, i+n)
}
}
@fabiolimace
fabiolimace / pure_pgsql_uuid7.sql
Created January 11, 2025 03:32
Pure SQL Function for Generating UUIDv7 on PostgreSQL
CREATE OR REPLACE FUNCTION uuid7_sql() RETURNS uuid AS $$
SELECT (FORMAT('%s-%s-%0s-%s-%s',
lpad(to_hex(trunc(EXTRACT(EPOCH FROM statement_timestamp()) * 1000)::bigint >> 16), 8, '0'),
lpad(to_hex(trunc(EXTRACT(EPOCH FROM statement_timestamp()) * 1000)::bigint & 65535), 4, '0'),
lpad(to_hex((trunc(random() * 2^12) + 28672)::bigint), 4, '0'), -- 28672 = 0x7000
lpad(to_hex((trunc(random() * 2^14) + 32768)::bigint), 4, '0'), -- 32768 = 0x8000
lpad(to_hex(trunc(random() * 2^48)::bigint), 12, '0')))::uuid;
$$ LANGUAGE SQL;
select uuid7_sql() -- 019450fe-7b0f-7ccc-8564-ad3ccc8234e0
@fabiolimace
fabiolimace / secure_random.sql
Last active January 11, 2025 14:57
Secure Random Functions for PostgreSQL using PGCRYPTO
-- install the extension
CREATE EXTENSION pgcrypto;
CREATE OR REPLACE FUNCTION secure_random_bigint() RETURNS bigint AS $$
DECLARE
v_bytes bytea;
v_value bigint := 0;
v_length integer := 8;
i integer := 0;
BEGIN