Created
November 22, 2016 07:18
-
-
Save ldante86/1e1665e8861487e52be56319f5f90e50 to your computer and use it in GitHub Desktop.
phone number formatter
This file contains 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/bash - | |
_format_phone_number() | |
{ | |
local one | |
local phone="$(tr -d '[:punct:]' <<< $1)" | |
if [[ $phone = *[!0-9]* ]] || | |
[ ${#phone} -lt 3 ] || | |
[ -z "$phone" ] | |
then | |
phone="" | |
fi | |
if [ "${phone:0:1}" = "1" ] && | |
[ ${#phone} -eq 11 ] | |
then | |
phone="${phone:1}" | |
one="1-" | |
fi | |
case ${#phone} in | |
6|7 ) phone="${phone:0:3}-${phone:3}" ;; | |
8|9|10 ) phone="${phone:0:3}-${phone:3:3}-${phone:6}" ;; | |
esac | |
echo "${one}${phone}" | |
} | |
_format_phone_number "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment