Skip to content

Instantly share code, notes, and snippets.

@blizzrdof77
Last active August 26, 2023 21:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blizzrdof77/1a0bff7ed7428666cdd20da2e3353b1a to your computer and use it in GitHub Desktop.
Save blizzrdof77/1a0bff7ed7428666cdd20da2e3353b1a to your computer and use it in GitHub Desktop.
String Modification Utility
#!/usr/bin/env bash
script_name='strmod'
script_title='String Modification Utility'
version=0.44.0
updated='2023-08-25'
if [ -f "$(which batcat)" ] && [ ! -z "$(which batcat)" ] && [ -z "$(which bat)" ]; then
BatCat="$(command -v batcat) --language=Manpage"
elif [ -f "$(which bat)" ] && [ ! -z "$(which bat)" ]; then
BatCat="$(command -v bat) --language=Manpage"
else
BatCat=$(command -v cat)
fi
all_args=''
all_vals=''
trueval='true'
falseval='false'
if [ "$1" = "-v" ] || [ "$1" = "--version" ] || [ "$1" = "version" ]; then
echo "${script_name} [${script_title}]"
echo "${version}"
echo "(updated ${updated})"
exit 0
elif [ "$1" = "--help" ] || [ "$1" = "-h" ] || [ "$1" = "help" ]; then
if [ "$2" = "-l" ] || [ "$2" = "--less" ]; then
HelpPipe=$(command -v less)
else
HelpPipe=$(command -v cat)
fi
curl -k -L -s --request GET "https://raw.githubusercontent.com/blizzrdof77/cheatsheets/master/strmod?version=$(date +%T | strmod replace ':' '')" | $BatCat | $HelpPipe
exit 0
fi
if [ "$1" = "get_type" ] || [ "$1" = "is_type" ]; then
if [ ! command -v jq &> /dev/null ]; then
echo "The subcommand \"$1\" requires the 'jq' package"
exit 1
else
MyJQ=$(command -v jq)
fi
fi
script_usage() {
usage=$(strmod --help | strmod line $(strmod --help | grep -E --line-number 'SYNOPSIS.*$' | strmod regreplace '([0-9]+).*' '$1' | strmod calculate '+1') | strmod trim)
echo "Usage:${usage}"
echo "Try \"${script_name} --help\" for more information." | strmod replace '"' "'"
}
math() {
local calc
calc="${@//p/+}"
calc="${calc//x/*}"
bc -l <<<"scale=10;$calc"
}
string_mod() {
local local_args
prefix=''
suffix=''
pass=$falseval
# For negating true/false operations
if [[ "$1" = "--not" ]] || [[ "$1" = "-n" ]]; then
trueval='false' && falseval='true'
shift
fi
if [ -z "$2" ]; then
if [ "$1" = "slugify" ]; then
xtra='_'
elif [[ "$1" = "join" || "$1" = "expand" ]] && [[ -z "$2" ]]; then
xtra=' '
else
xtra=''
fi
else
xtra="$2"
fi
result=$(awk '{ print }')
if [ "$1" = "trim" ]; then
echo "$result" | awk '{$1=$1};1'
elif [ "$1" = "join" ]; then
echo -e "${result}" | strmod regreplace "\n" "${xtra}"
elif [ "$1" = "collapse" ]; then
if [ "$2" = "--all" ] || [ "$2" = "-a" ]; then
echo "${result}" | sed -E '/^[[:space:]]*$/d'
else
echo "${result}" | awk NF
fi
elif [ "$1" = "expand" ]; then
echo -e ${result//$xtra/\\n}
elif [ "$1" = "to_upper" ] || [ "$1" = "uppercase" ]; then
echo "$result" | awk '{ print toupper($0); }'
elif [ "$1" = "to_lower" ] || [ "$1" = "lowercase" ]; then
echo "$result" | awk '{ print tolower($0); }'
elif [ "$1" = "to_title" ] || [ "$1" = "titlecase" ]; then
if [ $(echo "${result}" | strmod contains ' ') = "false" ]; then
result=$(echo "${result}" | strmod regreplace "[-_]" " ")
fi
echo "${result}" | perl -pe 's/\b[\p{L}\p{Pd}]+/ucfirst lc $&/ge'
echo "$result" | perl -pe 's/\b[\p{L}\p{Pd}]+/ucfirst lc $&/ge'
elif [ "$1" = "capitalize" ]; then
result=$(awk '{for (i=1; i<=NF; i++)
printf toupper(substr($i, 1, 1)) (substr($i,2)) " "; print ""}' <<< "$result")
echo $result
elif [ "$1" = "to_camel" ] || [ "$1" = "camelcase" ]; then
echo "$result" | perl -pe 's/\b[\p{L}\p{Pd}]+/ucfirst lc $&/ge' | strmod replace ' ' ''
elif [ "$1" = "slugify" ]; then
echo "$result" | iconv -c -t US-ASCII | sed -E "s/[']+//g" | sed -E 's/[~^]+//g' | sed -E "s/[^a-zA-Z0-9]+/${xtra}/g" | sed -E 's/^-+|-+$//g' | tr A-Z a-z
elif [ "$1" = "trim_suffix" ]; then
suffix="${xtra}"
echo "$result" | perl -pe "s/${prefix}(.*)${suffix}/\1/g"
elif [ "$1" = "trim_prefix" ]; then
prefix="${xtra}"
echo "$result" | perl -pe "s/${prefix}(.*)${suffix}/\1/g"
elif [ "$1" = "strip" ]; then
prefix="${xtra}"
if [ -z "$3" ]; then suffix="${xtra}"; else suffix="${3}"; fi
echo "$result" | perl -pe "s/${prefix}(.*)${suffix}/\1/g"
elif [ "$1" = "split" ]; then
results=$(perl -E 'say for split quotemeta shift, shift' -- "$xtra" "$result")
if [ -z "$3" ]; then
echo "$results"
else
count=$(echo "$results" | strmod lines | strmod calculate '-1')
action=$(echo "$3" | strmod replace 'first' '0' | strmod replace 'last' "$count" | strmod replace '-1' "$count" | strmod calculate '+1')
numeric=$(echo "$action" | strmod is_type number)
echo "$results" | strmod line "$action"
fi
elif [ "$1" = "slice" ]; then
part_1="${result%${xtra}*}"
part_2="${result##*${xtra}}"
if [ -z "$3" ]; then
echo $part_1 && echo $part_2
elif [ "$3" = "first" ] || [ "$3" = "0" ]; then
echo $part_1
elif [ "$3" = "last" ] || [ "$3" = "-1" ] || [ "$3" = "1" ]; then
echo $part_2
fi
elif [ "$1" = "replace" ] || [ "$1" = "find_replace" ] || [ "$1" = "regex_replace" ] || [ "$1" = "regreplace" ]; then
if [ "$1" = "regex_replace" ] || [ "$1" = "regreplace" ]; then
shift && echo "$result" | perl -pe "s/${1}/${2}/g"
else
f="${2}"
r="${3}"
s="${result}"
echo "${s//$f/$r}"
fi
elif [ "$1" = "remove" ] || [ "$1" = "without" ]; then
shift
for arg in "$@"; do
result=$(echo "$result" | strmod replace "${arg}")
done
echo "$result"
elif [ "$1" = "words" ]; then
echo -e "$result" | wc -w | strmod trim
elif [ "$1" = "length" ] || [ "$1" = "count" ] || [ "$1" = "chars" ]; then
if [ -z "$xtra" ]; then
all_counts=$(echo "$result" | awk '{ print length }')
else
all_counts=$(echo "$result" | grep -o "${xtra}" | strmod lines)
fi
if [ "$(echo \"${all_counts}\" | strmod contains ' ')" = "true" ]; then
all_counts=$(math "$(echo $all_counts | strmod replace ' ' '+')")
fi
echo $all_counts
elif [ "$1" = "line" ]; then
t=$(echo "$result" | strmod lines)
index=$(echo "$2" | strmod replace 'first' '1' | strmod regreplace '(last)|(-1)' "$t")
echo "$result" | sed -n "${index}p"
elif [ "$1" = "lines" ]; then
if [[ -z "$result" ]]; then
echo '0'
else
count=$(echo "$result" | wc -l | awk '{$1=$1};1')
if [ -z "$xtra" ]; then
echo $count
else
shift
for line in "$@"; do
echo "$result" | sed -n "${line}p"
done
fi
fi
elif [ "$1" = "sort" ]; then
if [ "$2" = "--reverse" ] || [ "$2" = "-r" ]; then
shift && echo "$result" | sort $@
else
echo "$result" | sort
fi
elif [ "$1" = "random" ] || [ "$1" = "shuffle" ]; then
echo "$result" | perl -MList::Util=shuffle -e 'print shuffle(<STDIN>);'
elif [ "$1" = "reverse" ]; then
if [ $(echo "$xtra" | strmod contains_any '-c' 'char' 'string') = "true" ] || [ $(echo "$result" | strmod lines) = "0" ]; then
echo "$result" | perl -ne 'chomp;print scalar reverse . "\n";'
else
echo "$result" | sort --reverse
fi
elif [ "$1" = "endswith" ]; then
pass=$(echo "$result" | grep ".*${xtra}$")
if [ -z "$pass" ]; then echo $falseval; else echo $trueval; fi
elif [ "$1" = "startswith" ]; then
pass=$(echo "$result" | grep "^${xtra}.*")
if [ -z "$pass" ]; then echo $falseval; else echo $trueval; fi
elif [ "$1" = "empty" ]; then
if [ -z "$result" ]; then echo $trueval; else echo $falseval; fi
elif [ "$1" = "contains" ]; then
pass=$(grep -o "$xtra" <<< "${result}" 2> /dev/null | grep -c . 2> /dev/null )
if [[ "$pass" = "0" ]]; then echo $falseval; else echo $trueval; fi
elif [ "$1" = "contains_any" ]; then
shift && declare -a matches=($@)
for match in "${matches[@]}"; do
test=$(echo "$result" | strmod contains "$match")
if [[ "$test" = "$trueval" ]]; then pass=$trueval; fi
done
echo $pass
elif [ "$1" = "equals" ]; then
if [ "${xtra}" = "${result}" ]; then echo $trueval; else echo $falseval; fi
elif [ "$1" = "equals_any" ]; then
shift && declare -a matches=($@)
for match in "${matches[@]}"; do
if [ "$(echo \"${result}\" | strmod equals \"${match}\")" = "${trueval}" ]; then
pass=$trueval
fi
done
echo $pass
elif [ "$1" = "greaterthan" ] || [ "$1" = "lessthan" ]; then
operator=$(echo "$1" | strmod replace 'greaterthan' '>' | strmod replace 'lessthan' '<')
echo $result "$operator" $xtra | bc -l | strmod equals 1
elif [ "$1" = "prepend" ]; then
prefix="$xtra"
echo "${prefix}${result}${suffix}"
elif [ "$1" = "append" ]; then
suffix="${xtra}"
echo -e "${prefix}${result}${suffix}"
elif [ "$1" = "wrap" ]; then
prefix="${xtra}"
if [ -z "$3" ]; then suffix="${xtra}"; else suffix="${3}"; fi
echo -e "${prefix}${result}${suffix}"
elif [ "$1" = "calculate" ]; then
calculation="${xtra}"
math "${result} ${calculation}"
elif [ "$1" = "round" ]; then
echo "$result" | awk '{print int($1+0.5)}'
elif [ "$1" = "get_type" ]; then
orig=$(echo "$result" | strmod to_lower)
if [ -n "$orig" ] && [ "$orig" -eq "$orig" ] 2>/dev/null; then
out='number'
elif [[ "$orig" = "true" || "$orig" = "false" ]]; then
out='boolean'
elif [ "$orig" = "null" ]; then
out="null"
elif [ "$(echo \"${orig}\" | strmod contains '[[{].*[]}]')" = "false" ]; then
out="string"
else
out=$(echo "${orig}" | $MyJQ '. | type' --raw-output)
fi
echo "$out"
elif [ "$1" = "is_type" ]; then
echo "${result}" | strmod get_type | strmod equals "${xtra}"
else
echo "${script_name}: unrecognized subcommand \"${1}\""
script_usage
exit 1
fi
}
action="${1}"
for arg in "$@"; do
if [[ "$arg" = "$action" ]]; then
all_args="${all_args} --${arg}"
shift
else
all_vals="${all_vals} \"${arg}\""
fi
done
string_mod $action "${@}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment