Last active
February 7, 2025 12:58
-
-
Save thingsiplay/a14a9a3d72006f958d1374e0da807af3 to your computer and use it in GitHub Desktop.
tocase - Change the lower and upper case characters.
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
#!/usr/bin/env bash | |
if [ "${#}" -eq 0 ] ; then | |
cat << EOF | |
usage: tocase option... | |
options: | |
upper all uppercase | |
upper1 upper first character | |
lower all lowercase | |
lower1 lower first character | |
toggle swap uppercase and lowercase | |
toggle1 swap upper and lower of first character | |
title upper first character and lower rest of each word | |
examples: | |
echo "Hello World, this is an EXAMPLE." | tocase toggle upper1 | |
EOF | |
exit 0 | |
fi | |
while IFS= read -r stdin ; do | |
for argument in "${@}" ; do | |
case "${argument}" in | |
upper) stdin="${stdin^^}" ;; | |
upper1) stdin="${stdin^}" ;; | |
lower) stdin="${stdin,,}" ;; | |
lower1) stdin="${stdin,}" ;; | |
toggle) stdin="${stdin~~}" ;; | |
toggle1) stdin="${stdin~}" ;; | |
title) | |
# Note: Many other solutions other than this sed command do not | |
# work on each word. | |
stdin="$(sed -r 's/\<./\U&/g' <<<"${stdin}")" | |
;; | |
esac | |
done | |
printf "%s\n" "${stdin}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment