[me@host]$ perl -ne 'push(@list, "($1) $2-$3") while /\(?(\d{3})?\)?[-.\s]*(\d{3})[-.\s]*(\d{4})/g; END{print join("\n",@list)."\n";}' data.txt () 209-4044 (440) 248-2700 (440) 975-0537 () 213-0518 (330) 836-9300 (330) 864-5741 (313) 884-6400 (313) 884-0600 (313) 590-9253 [me@host]$