Filter Phone Numbers in Two Exact Formats

Quick Overview

Write a one-line Bash script that filters file.txt for two exact phone-number formats. Practice full-line regular-expression matching, digit counts, literal parentheses, spaces, and hyphens.

Filter Phone Numbers in Two Exact Formats

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Write a one-line Bash script that reads `file.txt` and prints its valid phone-number lines to standard output. The file contains one phone-number candidate per line. A valid number has exactly one of these formats, where each `x` denotes a digit: ```text (xxx) xxx-xxxx xxx-xxx-xxxx ``` The parentheses, single space after the closing parenthesis, and hyphens must occur exactly as shown. Match the whole line, not a phone-number substring inside a longer line. ### Input and Output Read from `file.txt`. Print the matching lines themselves; do not print a Boolean, line numbers, or a count. The task is format validation, not verification that a number is assigned or callable. ### Examples **Example 1:** `file.txt`: ```text 415-203-9876 415 203 9876 (212) 908-7654 ``` Output: ```text 415-203-9876 (212) 908-7654 ``` The middle line uses spaces instead of the required hyphens. **Example 2:** `file.txt`: ```text (650) 120-3400 (650)120-3400 650-120-34000 650-120-3400 ``` Output: ```text (650) 120-3400 650-120-3400 ``` The second line lacks the required space. The third has an extra final digit. ### Constraints and Clarifications - Lines have no leading or trailing whitespace. - The two displayed formats define validity; no additional area-code or exchange-prefix restrictions are given. - The answer must be a one-line Bash script using `file.txt` as input. - No maximum file size or line length is specified. ### What a Strong Answer Covers - Accept both formats with exactly the required digit-group lengths. - Distinguish literal parentheses and the required space from pattern syntax. - Reject extra characters anywhere on an otherwise matching line. ### Follow-up Questions - What incorrect matches become possible if the beginning or end anchor is removed? - Why does making the space optional accept a format that this problem excludes?

Overview: Write a one-line Bash script that filters file.txt for two exact phone-number formats. Practice full-line regular-expression matching, digit counts, literal parentheses, spaces, and hyphens.

Amazon logo
Amazon
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Write a one-line Bash script that reads file.txt and prints its valid phone-number lines to standard output. The file contains one phone-number candidate per line.

A valid number has exactly one of these formats, where each x denotes a digit:

(xxx) xxx-xxxx
xxx-xxx-xxxx

The parentheses, single space after the closing parenthesis, and hyphens must occur exactly as shown. Match the whole line, not a phone-number substring inside a longer line.

Input and Output

Read from file.txt. Print the matching lines themselves; do not print a Boolean, line numbers, or a count. The task is format validation, not verification that a number is assigned or callable.

Examples

Example 1:

file.txt:

415-203-9876
415 203 9876
(212) 908-7654

Output:

415-203-9876
(212) 908-7654

The middle line uses spaces instead of the required hyphens.

Example 2:

file.txt:

(650) 120-3400
(650)120-3400
650-120-34000
650-120-3400

Output:

(650) 120-3400
650-120-3400

The second line lacks the required space. The third has an extra final digit.

Constraints and Clarifications

  • Lines have no leading or trailing whitespace.
  • The two displayed formats define validity; no additional area-code or exchange-prefix restrictions are given.
  • The answer must be a one-line Bash script using file.txt as input.
  • No maximum file size or line length is specified.

What a Strong Answer Covers Guidance

  • Accept both formats with exactly the required digit-group lengths.
  • Distinguish literal parentheses and the required space from pattern syntax.
  • Reject extra characters anywhere on an otherwise matching line.

Follow-up Questions Guidance

  • What incorrect matches become possible if the beginning or end anchor is removed?
  • Why does making the space optional accept a format that this problem excludes?