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的更多相关文章

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

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

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

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

  4. 193 Valid Phone Numbers

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  5. LeetCode 193. Valid Phone Numbers

    分析 难度 易 来源 https://leetcode.com/problems/valid-phone-numbers/ 题目 Given a text file file.txt that con ...

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

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. grub paramiter & menu.list

    在Linux中,给kernel传递参数以控制其行为总共有三种方法: 1.build kernel之时的各个configuration选项. 2.当kernel启动之时,可以参数在kernel被GRUB ...

  9. RFID 读写器 Reader Writer Cloner

    RFID读写器的工作原理 RFID的数据采集以读写器为主导,RFID读写器是一种通过无线通信,实现对标签识别和内存数据的读出和写入操作的装置. 读写器又称为阅读器或读头(Reader).查询器(Int ...

随机推荐

  1. Delphi Web Service和ISAPI的区别与联系 转

    Web Service和ISAPI的区别与联系   1.Web Service 是一种新的web应用程序分支,他们是自包含.自描述.模块化的应用,可以发布.定位.通过web调用.Web Service ...

  2. Hibernate(六)一对一双向关联映射

    在上次的博文Hibernate从入门到精通(五)一对一单向关联映射中我们讲解了一下一对一单向关联映射, 这次我们继续讲解一下与之对应的一对一双向关联映射. 一对一双向关联 与一对一单向关联映 射所不同 ...

  3. StringBuilder字符串缓冲区

    JDK1.5出现StringBuiler:构造一个其中不带字符的字符串生成器,初始容量为 16 个字符.该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候( ...

  4. This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery 解决方法

    This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'的意思是,这版本的 MySQL 不支持使 ...

  5. JavaScript高级程序设计16.pdf

    第8章 BOM BOM的核心对象就是window,它表示浏览器的一个实例,在浏览器中window对象有双重角色,它既是JavaScript访问浏览器的一个接口,又是规定的Global对象,因此所有在全 ...

  6. 《University Calculus》-chape6-定积分的应用-平面曲线长度

    平面曲线的长度: 积分的重要作用体现在处理曲线和曲面. 在这里我们讨论平面中一条用参数形式表达的曲线:x=f(t),y=g(t),a≤t≤b. 如图. y=f(x)形式的弧长计算: 之前我们讨论过平面 ...

  7. linux —— 启动引导程序 lilo 与 grub

    目录:1.启动引导程序概要 2.lilo 的安装与配置 3.grub的安装与配置 4.两种引导程序的切换  5.附:编译内核时的lilo 设置 1.启动引导程序概要 2.lilo 的安装与配置 3.g ...

  8. ListBoxControl 删除选择的项的方法

    public void RemoveSelectItems<T>(BaseListBoxControl listControl)        {            var list ...

  9. hadoop实例

    一篇讲得很好的hadoop实例,非常适合初学者学习hadoop. 本文转载自:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.ht ...

  10. Python3.x爬虫教程:爬网页、爬图片、自己主动登录

    林炳文Evankaka原创作品. 转载请注明出处http://blog.csdn.net/evankaka 摘要:本文将使用Python3.4爬网页.爬图片.自己主动登录.并对HTTP协议做了一个简单 ...