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 Code
awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt

1) awk '/^$/' file.txt => '^' 表示开始, '$' 表示结束

2) [0-9]表明数字0-9, {3}是三位

3) | 表明或

4) \(    与 \)里 '\' 是转译符, 表明会print

5) 其他同理

[LeetCode] 193. Valid Phone Numbers_Easy tag: Bash的更多相关文章

  1. LeetCode 193. Valid Phone Numbers

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

  2. [LeetCode] 367. Valid Perfect Square_Easy tag:Math

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  3. [LeetCode] 680. Valid Palindrome II_Easy tag: Two Pointers

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  4. [LeetCode] 728. Self Dividing Numbers_Easy tag: Math

    A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is ...

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

  6. [LeetCode] Longest Valid Parentheses

    第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...

  7. [LeetCode] 036. Valid Sudoku (Easy) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...

  8. LeetCode:36. Valid Sudoku,数独是否有效

    LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...

  9. [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

随机推荐

  1. liunx trac 邮件提示功能

    http://trac.edgewall.org/wiki/TracNotification官网上提供的方法.个人觉得不是清楚,不过还是有参考价值的.以下写下自己的添加过程,以作记录. 1.the [ ...

  2. 【WEB前端系列之CSS】CSS3动画之Tranition

    前言 css中的transition允许css的属性值在一定的时间区间内平滑的过渡.这种效果可以在鼠标点击.获得焦点.被点击或对元素任何改变中触发,并圆滑的以动画效果改变CSS的属性值.语法: tra ...

  3. 对Array进行排序(按字母顺序)

    通过使用java.utils.Arrays.sort()和String.CASE_INSENSITIVE_OREDR,可以很容易的对结果进行排序(按字母顺序): String[] array = { ...

  4. C和C++书籍推荐

    http://bestcbooks.com/recommend/most-influential-book/ http://www.ruanyifeng.com/blog/2011/09/c_prog ...

  5. linux常用命令大全3--rpm安装软件

    RPM 包 - (Fedora, Redhat,CentOS及类似系统) rpm -ivh package.rpm 安装一个rpm包 rpm -ivh --nodeeps package.rpm 安装 ...

  6. 提交代码到自己的github仓库

    说起来尴尬,好几年前就搞了github建了仓库,当时玩得还有点6,后来一直的公司都是svn,自己业务项目也没玩,都忘了要怎么提交代码到自己的仓库了. 这边再来一波记录吧. 一.配置用户名 git co ...

  7. 基于spring-cloud的微服务(3)eureka的客户端如何使用IP地址来进行注册

    例子中和我写的代码里,使用的spring-boot的版本是2.0 Eureka的客户端默认是使用hostname来进行注册的,有的时候,hostname是不可靠的,需要使用IP地址来进行注册 name ...

  8. Excel转Json

    参考: Excel2JSON Excel转JSON Excel另存为JSON的技巧  (office的插件) excel2json 游戏程序员的自我修养 (其他人写的工具) Excel转JSON格式- ...

  9. 【BZOJ3456】城市规划 多项式求逆

    [BZOJ3456]城市规划 Description 刚刚解决完电力网络的问题, 阿狸又被领导的任务给难住了. 刚才说过, 阿狸的国家有n个城市, 现在国家需要在某些城市对之间建立一些贸易路线, 使得 ...

  10. PHPStorm 注册码&主题皮肤

    JetBrains PhpStorm 注册方法: 用浏览器打开 http://idea.lanyus.com/ 点击页面中的“获得注册码” 然后打开PhpStorm,在注册时切换至Activation ...