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

题解:验证字符串是否为正确电话号码格式

解法一: awk

注:

  • awk '/正则表达式/' file.txt
  • [0-9]{3}: 表示 0~9数字匹配三次
  • ([0-9]{3}-|\([0-9]{3}\)): 表示为一组, | 表示为或
awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt 

解法二:grep

注意:\d{3}来表示[0-9]{3};-P:逐行匹配

grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$' file.txt 

解法三:sed

补充sed用法:

简介:sed是流编辑工具,用来对文本进行过滤和替换。sed通过输入读取文件内容,但 一次仅读取一行内容 进行某些指令处理后输出,sed更适合于处理大数据文件。

基本原理:sed在处理文本文件的时候,会在内存上创建一个模式空间,然后把这个文件的每一行调入模式空间用相应的命令处理,处理完输出;接着处理下一行,直到最后。

基本语法:

(1)sed [选项]  [定址commands] [inputfile]

关于定址:

  • 定址可以是0个、1个、2个;通知sed去处理文件的哪几行。
  • 0个:没有定址,处理文件的所有行
  • 1个:行号,处理行号所在位置的行
  • 2个:行号、正则表达式,处理被行号或正则表达式包起来的行

(2)选项:

 --version       显示sed版本hao

--help            显示帮助文档

-n                  关闭默认输出,默认将自动打印所有行

-e                  多点编辑,允许多个脚本指令被执行。

-r                  支持扩展正则+ ? () {} |

-i                   可以修改原文件,慎用!

-f                  支持使用脚本

命令:

       p         打印行

       d         删除行

       s         替换

       n         替换第几个匹内容

       w        另存为

       a         之后添加一行

       i         当前行之前插入文本

       y        替换匹配内容

(p和-n合用)

(d:删除)

(s/pattern/replacement/flags)

题解:

sed -n -r '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/p' file.txt 

LeetCode(193. Valid Phone Numbers)(sed用法)的更多相关文章

  1. LeetCode 193. Valid Phone Numbers

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

  2. 193 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_Easy tag: Bash

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

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

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

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

  6. linux学习基础6之sed用法详解

    1 sed 又称为流编辑器,它逐行将文本文件中的行读取到模式空间中间去,将符合编辑条件的行进行编辑后输出到显示器上来.默认sed不编辑原文件只处理模式空间中的内容. 2 sed用法 sed [opti ...

  7. [LeetCode] Longest Valid Parentheses

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

  8. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  9. Valid Phone Numbers

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

随机推荐

  1. bzoj 4542: [Hnoi2016]大数 (莫队)

    Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小B还有一个素数P.现在,小 B 提出了 M 个询问,每个 ...

  2. 爬虫_古诗文网(队列,多线程,锁,正则,xpath)

      import requests from queue import Queue import threading from lxml import etree import re import c ...

  3. BZOJ 2069: [POI2004]ZAW(Dijkstra + 二进制拆分)

    题意 给定一个有 \(N\) 个点 \(M\) 条边的无向图, 每条无向边 最多只能经过一次 . 对于边 \((u, v)\) , 从 \(u\) 到 \(v\) 的代价为 \(a\) , 从 \(v ...

  4. 【CF809D】Hitchhiking in the Baltic States(Splay,动态规划)

    [CF809D]Hitchhiking in the Baltic States(Splay,动态规划) 题面 CF 洛谷 题解 朴素\(dp\):设\(f[i][j]\)表示当前考虑到第\(i\)个 ...

  5. 如何在代码中减少if else语句的使用

    前言 代码中嵌套的if/else结构往往导致代码不美观,也不易于理解.面向过程的开发中代码有大量的if else,在java中可以用一些设计模式替换掉这些逻辑,那么在js中是否也有类似的方法用来尽可能 ...

  6. noiac132 B君的第三题 (树形dp)

    传送门 本来想用点分治做,结果root又求不对 算的时候还算错了 我好菜啊 结果szr大佬告诉我是树形dp 我好菜啊!! 我们有$\lceil \frac{x}{k} \rceil = \frac{x ...

  7. CF670C cinema

    想必是个半水题,div2的C嘛 仔细观察,发现排序可做. 怎么排序呢?排啥呢?拿啥离散化,拿啥结构体呢? 仔细思考热静分析,便可得出结论: 以每个人会的语言离散化,把每个电影建结构体后不排序,而是枚举 ...

  8. snmpwalk

    什么是snmpwalk?snmpwalk是一个SNMP小程序,它使用SNMP的GETNEXT请求查询指定OID(SNMP协议中的对象标识)入口的所有OID树信息,并显示给用户. snmpwalk的作用 ...

  9. 腾讯云centos安装python3.6和pip

    不知道腾讯云的centos和阿里云的centos一不一样,反正两个云平台的Ubuntu系统是不一样的,照着同样的教程敲,往往掉坑里. 安装一些centos依赖库: 这一步很关键,很多报错往往都因为少了 ...

  10. django基于restframework的CBV封装

    一.models数据库映射 from django.db import models # Create your models here. class Book(models.Model): titl ...