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. java(2) 面向对象

    1.类的封装 *在定义一个类时,将类中的属性私有化,即使用prviate关键字来修饰,私有属性只能在它所在的类中被访问.为了能让外界访问私有属性,需要提供一些使用public修饰的公有方法,其中包括用 ...

  2. 严版数据结构题集2.13 & 2.14

    1.试写一算法在带头结点的单链表结构上实现线性表操作Locate(L,x) 2.试写一算法在带头结点的单链表结构上实现线性表操作Length(L) #include<stdio.h> #i ...

  3. C# IEqualityComparer 去重

    1.去除list里某重复字段值的数据(相当于group by) public class CorrController { //方法 public void DoGet() { List<tes ...

  4. web.xml之context-param,listener,filter,servlet加载顺序及其周边

    先以加载spring为例子看看加载顺序的作用: Spring加载可以利用ServletContextListener 实现,也可以采用load-on-startup Servlet 实现,但比如fil ...

  5. Zabbix监控Nginx状态信息

    首先要检查Nginx是否安装了 http_stub_status_module 模块,通过下面的命令可以看到编译参数.yum安装的默认会带有这个模块. [root@kafka60 ~]# /data/ ...

  6. 使用Xstart远程图形化Linux

    进入桌面以后su - 输入密码切换到root用户 rcc命令调出RoseMirrorHa页面进行操作

  7. Saltstack之api

    Salt-API 1,htps证书 2,配置文件 3,验证.使用PAM验证 4,启动salt-api 安装salt-api yum -y install salt-api 创建用户 useradd - ...

  8. Saltstack生产案例之Haproxy安装

    cd /srv/salt/prod/ mkdir haproxymkdir keepalivedmkdir nginxmkdir phpmkdir memcachedmkdir pkg cd pkg ...

  9. hdu1540 Tunnel Warfare【线段树】

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...

  10. 详解Oracle多种表连接方式

    1. 内连接(自然连接) 2. 外连接 (1)左外连接 (左边的表不加限制) (2)右外连接(右边的表不加限制) (3)全外连接(左右两表都不加限制) 3. 自连接(同一张表内的连接) SQL的标准语 ...