saadurr
saadurr

Reputation: 77

Bash: Scraping a text file to find phone numbers using bash script

I am trying to write a script using grep to find phone numbers in a text format.

The format for phone numbers is:

(XXXXX) XXXXXX
 XXXXX XXXXXX
(XXXXX)XXXXXX

I have written the following script but it doesnt seme to work:

grep -E -o "\b[0-9]\{5\}[0-9]\{6\}\b" webpage.html|
while read phone
do
    echo "$phone" >> testp.txt
done

Can someone help?

Upvotes: 0

Views: 899

Answers (1)

Raman Sailopal
Raman Sailopal

Reputation: 12877

grep -Eo '\(?[[:digit:]]{5}\)?[[:space:]]?[[:digit:]]{6}' webpage.html > testp.txt

You will need to escape the opening and closing brackets. Search for and opening bracket 0 or 1 times, then 5 digits, a space one or more times and digits 6 times.

Upvotes: 1

Related Questions