Skip to content

Instantly share code, notes, and snippets.

@ldante86
Created November 22, 2016 07:18
Show Gist options
  • Save ldante86/1e1665e8861487e52be56319f5f90e50 to your computer and use it in GitHub Desktop.
Save ldante86/1e1665e8861487e52be56319f5f90e50 to your computer and use it in GitHub Desktop.
phone number formatter
#!/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