Skip to content

Commit e691ff2

Browse files
committed
193: Valid Phone Numbers; solution.
1 parent 6345aaa commit e691ff2

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

bash/src/193-valid-phone-numbers.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
##
2+
# 193
3+
# Valid Phone Numbers
4+
##
5+
# Given a text file file.txt
6+
# that contains a list of phone numbers (one per line),
7+
# write a one-liner bash script to print all valid phone numbers.
8+
#
9+
# You may assume
10+
# that a valid phone number must appear
11+
# in one of the following two formats:
12+
# (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)
13+
#
14+
# You may also assume each line in the text file
15+
# must not contain leading or trailing white spaces.
16+
#
17+
# Example:
18+
# Assume that file.txt has the following content:
19+
# 987-123-4567
20+
# 123 456 7890
21+
# (123) 456-7890
22+
#
23+
# Your script should output the following valid phone numbers:
24+
# 987-123-4567
25+
# (123) 456-7890
26+
##
27+
# https://leetcode.com/problems/valid-phone-numbers/
28+
##
29+
30+
#!/bin/bash
31+
32+
# Read from the file file.txt and output all valid phone numbers to stdout.
33+
grep "\(^[0-9]\{3\}-[0-9]\{3\}-[0-9]\{4\}$\)\|\(^([0-9]\{3\}) [0-9]\{3\}-[0-9]\{4\}$\)" file.txt

0 commit comments

Comments
 (0)