Back to blog
Jul 29, 2025
8 min read

Valid Phone Numbers

Given a text file, print all valid phone numbers that match specific formats.

Difficulty: Easy | Acceptance: 29.60% | Paid: No Topics: Shell

Given a text file file.txt that contains list of phone numbers (one per line), print all valid phone numbers.

A valid phone number takes one of two formats:

  1. (xxx) xxx-xxxx
  2. xxx-xxx-xxxx

where x is a digit.

Examples

Example 1

Input:

987-123-4567
123 456 7890
(123) 456-7890

Output:

987-123-4567
(123) 456-7890

Example 2

Input:

-- 1-293-489-4859
(02) 3456-7890
(123) 456-7890

Output:

(123) 456-7890

Constraints

There may be empty lines in the text file.

Grep with Regular Expression

Intuition Use grep with a regular expression to match lines that follow either of the two valid phone number formats.

Steps

  • Construct a regex pattern that matches both formats
  • Use grep to filter lines matching the pattern
python
import re
import sys

pattern = re.compile(r'^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$|^[0-9]{3}-[0-9]{3}-[0-9]{4}$')
for line in sys.stdin:
    line = line.strip()
    if pattern.match(line):
        print(line)

Complexity

  • Time: O(n) where n is the number of lines
  • Space: O(1) excluding input storage
  • Notes: Simple and efficient for filtering lines

Awk with Pattern Matching

Intuition Use awk’s pattern matching capability to filter lines that match the phone number formats.

Steps

  • Define the pattern for valid phone numbers
  • Print lines that match the pattern
python
import sys

def is_valid_format1(s):
    if len(s) != 14:
        return False
    if s[0] != '(' or s[4] != ')' or s[5] != ' ' or s[9] != '-':
        return False
    return s[1:4].isdigit() and s[6:9].isdigit() and s[10:].isdigit()

def is_valid_format2(s):
    if len(s) != 12:
        return False
    if s[3] != '-' or s[7] != '-':
        return False
    return s[0:3].isdigit() and s[4:7].isdigit() and s[8:].isdigit()

for line in sys.stdin:
    line = line.strip()
    if is_valid_format1(line) or is_valid_format2(line):
        print(line)

Complexity

  • Time: O(n) where n is the number of lines
  • Space: O(1) excluding input storage
  • Notes: Manual character-by-character validation without regex

Sed with Substitution

Intuition Use sed to filter lines by deleting lines that don’t match the phone number pattern.

Steps

  • Define the pattern for valid phone numbers
  • Use sed to print only matching lines
python
import sys

def check_digits(s, indices):
    for i in indices:
        if not s[i].isdigit():
            return False
    return True

def is_valid(s):
    n = len(s)
    if n == 14:
        if s[0] == '(' and s[4] == ')' and s[5] == ' ' and s[9] == '-':
            return check_digits(s, [1, 2, 3, 6, 7, 8, 10, 11, 12, 13])
    if n == 12:
        if s[3] == '-' and s[7] == '-':
            return check_digits(s, [0, 1, 2, 4, 5, 6, 8, 9, 10, 11])
    return False

for line in sys.stdin:
    line = line.strip()
    if is_valid(line):
        print(line)

Complexity

  • Time: O(n) where n is the number of lines
  • Space: O(1) excluding input storage
  • Notes: Alternative approach using manual validation with index checking