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
主要考察正则表达式的写法和awk的使用
awk '$0 ~ /^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$/ {print $0}' file.txt
遇到的问题:
1、$0就表示整行记录,因此不用使用FS=“\n”,$1来操作
2、awk的正则
先看下一段代码
awk '$0 ~ /^\d{3}-\d{3}-\d{4}$|^\(\d{3}\) \d{3}-\d{4}$/ {print $0}' file.txt
运行会发现是错误的,因此推测\d在awk是非法的
3、正则表达式的分支(|)
4、^和$,如果不加会出错,比如下面的实例
0(101) 001-1223
0123-456-7891
123-456-78910
(001) 345-00001
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 bas ...
- [Bash]LeetCode193. 有效电话号码 | Valid Phone Numbers
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- LeetCode(193. Valid Phone Numbers)(sed用法)
193. Valid Phone Numbers Given a text file file.txt that contains list of phone numbers (one per lin ...
- 193 Valid Phone Numbers
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- LeetCode 193. Valid Phone Numbers
分析 难度 易 来源 https://leetcode.com/problems/valid-phone-numbers/ 题目 Given a text file file.txt that con ...
- [LeetCode] 193. Valid Phone Numbers_Easy tag: Bash
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- grub paramiter & menu.list
在Linux中,给kernel传递参数以控制其行为总共有三种方法: 1.build kernel之时的各个configuration选项. 2.当kernel启动之时,可以参数在kernel被GRUB ...
- RFID 读写器 Reader Writer Cloner
RFID读写器的工作原理 RFID的数据采集以读写器为主导,RFID读写器是一种通过无线通信,实现对标签识别和内存数据的读出和写入操作的装置. 读写器又称为阅读器或读头(Reader).查询器(Int ...
随机推荐
- zabbix web场景模拟监控配置
一,zabbix2.2.3 VMware Vsphere exsi监控配置步骤, 1,添加监控主机 2,添加聚集macro;{$PASSWORD} = yoodo.com{$URL} = http:/ ...
- pecl/mongo is already installed
sw-engine-cgi PHP MongoDB database drivermongodb database driver,数据库驱动;
- [Oracle] Data Pump 详细使用教程(4)- network_link
[Oracle] Data Pump 详细使用教程(1)- 总览 [Oracle] Data Pump 详细使用教程(2)- 总览 [Oracle] Data Pump 详细使用教程(3)- 总览 [ ...
- C#截取指定字符串函数
本文转载:http://www.cnblogs.com/liufei88866/archive/2012/05/12/2497395.html 一.通过函数方式进行获取. public string ...
- HDOJ 4696 Answers 乱搞
乱搞: rt.有1就能输出全部的数,否则仅仅能输出偶数 Answers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/1 ...
- Unix/Linux 脚本中 “set -e” 的作用
----------------------------------------------------------- #!/bin/bash set -e command 1 command 2 . ...
- CMake高速入门
入门基础:http://www.ibm.com/developerworks/cn/linux/l-cn-cmake/ 在 linux 下使用 CMake 构建应用程序 入门进阶:http ...
- Android 图标上面添加提醒(一)使用Canvas绘制
版权声明:本文为博主原创文章,未经博主允许不得转载. 在我们开发一些如通讯录.社交等应用或者应用添加新功能模块时,会考虑在对应的图标上加上未读信息的数量,或者是新功能提醒的图标,这样不占太大空间还能达 ...
- HDU-4041-Eliminate Witches! (11年北京网络赛!!)
Eliminate Witches! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Netty4具体解释二:开发第一个Netty应用程序
既然是入门,那我们就在这里写一个简单的Demo,client发送一个字符串到server端,server端接收字符串后再发送回client. 2.1.配置开发环境 1.安装JDK 2.去官网下 ...