Valid Phone Numbers

  • Problem
    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.
    You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)
    You may also assume each line in the text file must not contain leading or trailing white spaces.
    For example, assume that file.txt has the following content:
    987-123-4567
    123 456 7890
    (123) 456-7890
    Your script should output the following valid phone numbers:
    987-123-4567
    (123) 456-7890

  • code
    grep -P "^(\d{3}-|(\d{3}) )\d{3}-\d{4}$" file.txt

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容