?LeetCode刷題實(shí)戰(zhàn)193:有效電話號碼
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)
題意
示例
示例:
假設(shè) file.txt 內(nèi)容如下:
987-123-4567
123 456 7890
(123) 456-7890
你的腳本應(yīng)當(dāng)輸出下列有效的電話號碼:
987-123-4567
(123) 456-7890
解題
grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$' file.txt
思路二:sed命令
sed -n -r '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/p' file.txt
評論
圖片
表情
