Original: https://leetcode.com/problems/valid-phone-numbers/Given A text file file.txt that contains list of phones numbers (one per line), write a one Liner bash script to print All valid phone numbers. Assume that a valid phone number must appearinchOne of the following, formats: (XXX) xxx-xxxx or xxx-xxx-xxxx. (x means a digit) you may also assume all lineinchThe text file must not contain leading or trailing white spaces. For example, assume this file.txt has the following content:987-123-4567123 456 7890(123)456-7890Your script should output the following valid phone numbers:987-123-4567(123)456-7890simply put, that is, as long as the two are qualified to meet the number, the output can be.
Answer
Cat file. txt | grep " (^\ ([0-9]{3}\) [0-9]{3}-[0-9]{4}$] | (^ ([0-9]{3}-) {2}[0-9]{4}$) "
This uses the GREP-E command, which represents the use of a regular in the command.
The regular has ^ (indicates the beginning) and $ (for the end), [0-9] The number that represents from 0 to 9 consists of 0 and 9,{n} that matches n times.
where \ (\) indicates that the parentheses are escaped.
Shell Practice Verification Number