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. MT【290】内外圆求三角最值

    求$\sqrt{\dfrac{5}{4}-\sin x}+2\sqrt{\dfrac{9}{4}+\cos x-\sin x}$的最小值. 提示:$\sqrt{\dfrac{5}{4}-\sin x} ...

  2. Ability

    Base:网络安全,sklearn(ML),日本語,企业存储 Branch1:自动化,Git Branch2:HW系统架构 Branch3:shadowsocks源码:

  3. bzoj3702/bzoj2212 二叉树 (线段树合并)

    用线段树记每个子树中包含的数,然后合并的时候算出来逆序对的数量(合并a,b时,就是size[ch[a][1]]*size[ch[b][0]]),来决定这个子树要不要翻转 #include<bit ...

  4. Nginx反向代理后端多节点下故障节点的排除思路

    仔细想来,其实是个非常简单的问题:开发和运维觉得两个后端节点跑起来压力太大了,就扩充了两个新的后端节点上去,这一加就出问题了,访问时页面间歇性丢失,这尼玛什么情况...想了半天没思路,查了Nginx的 ...

  5. Redis基础、高级特性与性能调优

    本文将从Redis的基本特性入手,通过讲述Redis的数据结构和主要命令对Redis的基本能力进行直观介绍.之后概览Redis提供的高级能力,并在部署.维护.性能调优等多个方面进行更深入的介绍和指导. ...

  6. Electron入门笔记(一)-自己快速搭建一个app demo

    Electron学习-快速搭建app demo 作者: 狐狸家的鱼 Github: 八至 一.安装Node 1.从node官网下载 ,最好安装.msi后缀名的文件,新手可以查看安装教程进行安装. 2. ...

  7. AtCoder arc061C Snuke's Subway Trip

    大意: 给你一张无向图,边有种类. 当你第一次/重新进入某种边时费用 + 1 在同一种边之间行走无费用. 求 1 到 n 的最小费用. 嗯...乍一看有一个很直观的想法:记录每个点的最短路的上一条边的 ...

  8. 【POJ2182】Lost Cows 树状数组+二分

    题中给出了第 i 头牛前面有多少比它矮,如果正着分析比较难找到规律.因此,采用倒着分析的方法(最后一头牛的rank可以直接得出),对于第 i 头牛来说,它的rank值为没有被占用的rank集合中的第A ...

  9. 全面理解虚拟DOM(1)

    最近一两年前端最火的技术莫过于 reactjs,angularJS,vuejs,即便你没用过也可能听过,像ReactJS由业界顶尖的互联网公司facebook提出,其本身有很多先进的设计思路,比如页面 ...

  10. plink提取指定样本的数据(keep函数)

    提取样本见命令行: plink --bfile file --noweb --keep sampleID.txt --recode --make-bed --out sample 其中,sampleI ...