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 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.

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
# Read from the file file.txt and output all valid phone numbers to stdout.
egrep ‘(^\([0-9]{3}\) |^[0-9]{3}-)[0-9]{3}-[0-9]{4}$‘ file.txt

原文地址:https://www.cnblogs.com/bernieloveslife/p/10263114.html

时间: 2024-10-08 00:00:21

193. Valid Phone Numbers的相关文章

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] 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

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

【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

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

【leetcode】bash脚本练习

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } span.Apple-tab-span { white-space: pre } [192]Word Frequency Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity sake, you may assume: w

mtd子系统----设备层

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