Valid Phone Numbers

Source: Internet
Author: User

Given a text file file.txt that contains list of phones numbers (one per line), write a one Liner bash script to print all Val ID phone numbers.

You could assume that a valid phone number must appear in one of the following both formats: (XXX) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)

You could also assume each line in the text file must not contain leading or trailing white spaces.

For example, assume. has the file.txt following content:

987-123-4567123 456 7890 (123) 456-7890

Your script should output the following valid phone numbers:

987-123-4567 (123) 456-7890
The main study of the expression of regular expressions and the use of awk
awk ' $ ~/^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\ ([0-9]{3}\) [0-9]{3}-[0-9]{4}$/{print $} ' file. txt

Problems encountered:

1, the whole row of records, so do not use fs= "\ n", $ to operate

2. Awk's regular

First look at the next piece of code

awk ' $ /^\d{3}-\d{3}-\d{4}$|^\ (\d{3}\) \d{3}-\d{4}$/{print $} ' file. txt

Run will be found to be wrong, so speculate that \d is illegal in awk

3, the branch of the regular expression (|)

4, ^ and $, if no error is added, such as the following instance

0 (101) 001-1223

0123-456-7891

123-456-78910

(001) 345-00001

Valid Phone Numbers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.