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 phone-t5.txt has the following content:
134-1056-4490
590-2901-989
10-118
190-2020-2
a-1103
190-2234-8903
199-2-a
19910003
Your script should output the following valid phone numbers:
134-1056-4490
190-2234-8903
#!/bin/bash
sed -n -r '/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/p' phone-t5.txt
输出结果:
root@ubuntu:~# ./test.sh
134-1056-4490
190-2234-8903
awk '$1 ~ /^[0-9]{3}-[0-9]{4}-[0-9]{4}$/ { print }' phone.txt
#sed -n -r '/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/p' phone.txt
执行输出不对,The reason it did not work is because it was using mawk as the interpreter (which came as the default in Ubuntu)