Valid Phone Numbers

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 file.txt has the following content:

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

Your script should output the following valid phone numbers:

987-123-4567
(123) 456-7890

关键的地方在于使用正则表达式组匹配行首,注意只有ERE才支持正则表达式组,awk默认是ERE,grep 中使用参数-E指定ERE,sed使用-r指定ERE
# Read from the file file.txt and output all valid phone numbers to stdout.
#grep -E ‘^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$‘ file.txt
#awk ‘/^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$/‘ file.txt
sed -r -n ‘/^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$/p‘ file.txt
时间: 2024-08-25 08:11:22

Valid Phone Numbers的相关文章

[LeetCode] Valid Phone Numbers 验证电话号码

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

[LeetCode] Valid Phone Numbers

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

193. Valid Phone Numbers

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

LeetCode(193. Valid Phone Numbers)(sed用法)

193. Valid Phone Numbers 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 f

Leetcode 193.Valid Phone Numbers

https://leetcode.com/problems/valid-phone-numbers/description/Shell脚本题目:给file.txt ,输出复合格式的电话号码.思路是使用正则,但是有几个坑. 注意grep -E 与grep -P 的区别 https://www.jianshu.com/p/e1acfb7989b2-E 其实是扩展支持| 与& 这种方式,-P才是Perl正则 注意正则使用^ 和 $ 包围精确匹配 刚开始使用cat 和read 读取文件,发现要么多个空格

【LeetCode】关于shell的几个问题

195. Tenth Line 输出file.txt中的第十行 答案: # Read from the file file.txt and output the tenth line to stdout. #!/bin/bash sed -n '10p' file.txt 193. Valid Phone Numbers 找出file.txt中符合(xxx) xxx-xxxx or xxx-xxx-xxxx格式的电话号码 答案: 1 cat file.txt | grep -Eo '^(\([0

mtd子系统----设备层

设备层是实现了文件系统与Flash之间的桥梁,其基于MTD原始层提供了两种上层访问Flash的方式:MTD的字符设备和块设备,字符设备通过向内核注册字符设备的file_operations结构实现了对MTD设备的读写和控制,提供了对闪存的原始字符访问,关联的设备是/dev/mtd*,而MTD块设备则是定义了一个描述MTD块设备mtdblock_tr的结构,关联的设备是/dev/mtdblock*,下面主要看看其实现的原理. 1. MTD字符设备 对于MTD的字符设备,主要集中在driver/mt

Telephone Number

Problem Print all valid phone numbers of length n subject to following constraints:If a number contains a 4, it should start with 4No two consecutive digits can be sameThree digits (e.g. 7,2,9) will be entirely disallowed, take as input Solution Recu

leetcode 上的 bash 程序

*/--> pre.src {background-color: Black; color: White;} leetcode 上的 bash 程序 Table of Contents Tenth Line Transpose File Valid Phone Numbers Word Frequency 注: 以下程序大部份从 leetcode 的讨论上看来的... Tenth Line How would you print just the 10th line of a file? For