Skip to content

Instantly share code, notes, and snippets.

@oguz-ismail
oguz-ismail / prog.awk
Created January 31, 2021 09:40
Splitting a file with a very long comma separated string of numbers into multiple files
# https://stackoverflow.com/q/65964916
# Usage: awk -v limit=17577 -f prog.awk file
BEGIN {
RS = ORS = ","
}
{
nread++
}
@oguz-ismail
oguz-ismail / foo
Last active February 22, 2021 06:26
Code generation - Brace expansion
#!/bin/bash -
gen_sfx()
case $ext in
(/*|*//*|*/)
printf '%q: %q: Invalid specifier\n' "$0" "$ext" >&2
exit 3 ;;
(*/*)
local - IFS=/
set -f
@oguz-ismail
oguz-ismail / p2k
Last active April 28, 2023 05:52
rename pascal case to kebab case recursively
#!/bin/sh -
find . \
-type f \
-name '[A-Z]*' \
! -name '*[!0-9A-Za-z]*.*' \
-exec sh -c '
for pn; do
p=${pn%/*}/
fn=${pn#"$p"}
n=${fn%.*}
@oguz-ismail
oguz-ismail / b
Last active August 29, 2022 12:42
number to binary
LC_ALL=C
case $1 in
''|[!0-9]*|[!0]*[!0-9]*|0[!0-7Xx]*|0[Xx]|0[!Xx]*[!0-7]*|0[Xx]*[!0-9A-Fa-f]*)
printf '%s: invalid number: %s\n' "$0" "$1" >&2
exit 1
esac
x=$(($1)) bx=
while test $x -gt 0; do
@oguz-ismail
oguz-ismail / tree.jq
Last active September 8, 2022 08:10
reconstruct binary tree from its pre-order and in-order traversals
def tree($pre; $in):
def root($pre; $in):
def branch($pre; $in):
$in | if has(0) then
(index($pre[0]) + 1) as $i | {
l: root($pre[:$i]; .[:$i]),
r: root($pre[$i:]; .[$i:])
}
else
{l: {}, r: {}}
@oguz-ismail
oguz-ismail / deptab.gawk
Last active September 12, 2022 17:39
awk -f deptab.gawk /var/lib/dpkg/status
BEGIN {
FS = ": "
}
$1 == "Package" {
pkg = $2
tab[pkg][""]
}
$1 == "Status" {
@oguz-ismail
oguz-ismail / nat.awk
Last active September 24, 2022 07:37
columNATe
{
item[NR] = $0
itemw[NR] = length
}
END {
if (NR == 0)
exit
if (width == 0)
printf '%s\n' "$@" \
| awk '
{
sub(/boards\.4chan(nel)?/, "a.4cdn")
print "url=" $0 ".json"
}
' \
| curl -sS -K - -w '"%{url}"' \
| jq -nr '
def url($board):
@oguz-ismail
oguz-ismail / ipaste
Last active November 13, 2022 10:29
paste images side by side
err_usage() {
printf 'Usage: %s [-c columns] [-o outfile] image image...\n' "$0" >&2
exit 1
}
columns=0
out_file=images.jpg
while getopts :c:o: opt; do
case $opt in
@oguz-ismail
oguz-ismail / shwal
Created October 13, 2022 07:48
generate color scheme from image for tilix
err_usage() {
printf 'Usage: %s [-l] [-n name] [-c comment] image\n' "$0" >&2
exit 1
}
name=Custom
comment=
light_theme=0
while getopts :n:c:l opt; do