The pointy-haired manager\'s secretary, Pat, keeps a telephone directory for him
ID: 3870955 • Letter: T
Question
The pointy-haired manager's secretary, Pat, keeps a telephone directory for him. But Pat mixes up local phone numbers, long distance phone numbers, and even international numbers in it. Worse, Pat has no common method of entering numbers, some area codes are surrounded by ( ), some are not. Some numbers have a space between them, others a dash. The pointy-hair manager only wants to see the file with the names and their North American number plan numbers in it – the familiar seven digit number. (Those are numbers that end with 3 numbers, a space or dash, and then 4 numbers). Work out a command structure that can be issued to the file telephonefile_P2, that returns the following:
Sam Spade (212)-756-1045
Daffy Duck 312 450 2856
Mom 354-2015
Lolita Spengler (816) 756 8657
Dr. Harold Kranzler 765-986-9987
Ralph Spoilsport's Motors 967 882 6534
Sister Sue 978 0672
JCCC 913-469-8500
PLEASE ANSWER IN A UNIX COMMAND STRUCTURE!
Explanation / Answer
Ans:
cat pattern.txt | grep '[0-9]{3}[-," "][0-9]{4}$'
Above is the command to filter out the telephone number with the above condition.
Let's break out the above command.
We will use "cat" command to read the file and "grep" command which is used to search
the text/pattern in a file/stream. Here, '|' for streaming the content of the file to
"grep"
As per the question conditions says "numbers that end with 3 numbers, space or dash, and then 4 numbers"
1. To select only numbers we will use [0-9], where '-' inside the bracket includes numbers through 0 to 9.
2. To repeat this for next 3 digits we will use {3} here where '' to represent escape sequence '{'.
3. Similarly for selecting space and hyphen enclosed within bracket ',' is used for 'or' condition.
4. As per the similar step 1 and 2 just replace with 4.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.