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