Last active
November 26, 2021 00:33
-
-
Save JaHIY/4d934b28c8284742057c to your computer and use it in GitHub Desktop.
String-related functions in POSIX shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh - | |
substr() { | |
awk -v s="$1" -v i="$2" -v l="$3" 'BEGIN { print substr(s, i+1, l); }' | |
} | |
index() { | |
awk -v s="$1" -v c="$2" 'BEGIN { print index(s, c) - 1; }' | |
} | |
trim() { | |
tr -d '[:blank:]' | |
} | |
length() { | |
printf '%s' "$1" | wc -m | trim | |
} | |
ord() { | |
LC_CTYPE=C printf '%d' "'$1" | |
} | |
chr() { | |
[ "$1" -lt 256 ] || return 1 | |
printf '%b' "$(printf '\%04o' "$1")" | |
} | |
repeat() { | |
printf '%*s' "$2" | sed -e "s/[[:blank:]]/$1/g" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment