Consider using variables to reduce duplication, and enforce that the two options are mutually exclusive (which I think is what your description says). I also escaped the period in your email regex with \.
:
#!/bin/sh
while getopts 'e:p:' option
do
case "$option" in
e)
regex="\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b"
file=$OPTARG
;;
p)
regex="^((\([0-9]{3}\) )|([0-9]{3}\-))[0-9]{3}\-[0-9]{4}$"
file=$OPTARG
;;
esac
done
if [ -z "$regex" ]
then
# error handling
fi
egrep -o "$regex" "$file"
Use arguments instead of options if email or phone is required (sneaked in alternative long names for documentation):
#!/bin/sh
case "$1" in
e|email)
regex="\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b"
;;
p|phone)
regex="^((\([0-9]{3}\) )|([0-9]{3}\-))[0-9]{3}\-[0-9]{4}$"
;;
esac
if [ -z "$regex" ]
then
echo e or p required
exit 1
fi
if [ -z "$2" ]
then
echo file required
exit 1
fi
file=$2
egrep -o "$regex" "$file"
I reverse engineered your regex to generate the sample data that I asked you for earlier:
cat >input.txt
a bc@de.fg h
(123) 456-7890
123-456-7890
^D
and here is the result of running the 2nd script over the test data:
./Myfile.sh e input.txt
bc@de.fg
./Myfile.sh p input.txt
(123) 456-7890
123-456-7890
Btw, it's a good idea to leave out the .sh suffix. This allows you to seamless rewrite your program in another language should the need arise.