LeetCode(193. Valid Phone Numbers)(sed用法)
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用法)的更多相关文章
- LeetCode 193. Valid Phone Numbers
分析 难度 易 来源 https://leetcode.com/problems/valid-phone-numbers/ 题目 Given a text file file.txt that con ...
- 193 Valid Phone Numbers
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [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 ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [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 ...
- linux学习基础6之sed用法详解
1 sed 又称为流编辑器,它逐行将文本文件中的行读取到模式空间中间去,将符合编辑条件的行进行编辑后输出到显示器上来.默认sed不编辑原文件只处理模式空间中的内容. 2 sed用法 sed [opti ...
- [LeetCode] Longest Valid Parentheses
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- Valid Phone Numbers
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
随机推荐
- QML 用QSortFilterProxyModel实现搜索功能
搞了一晚上终于实现了,写个博客纪念一下吧. c++部分的代码: #include <QQmlApplicationEngine> #include <QQmlContext> ...
- HNOI2019总结
HNOI2019总结 Day 1 开场看三道题,T1是个计算几何,T2是个操作树加\(border\),T3题意有点复杂.想T1想了半个多小时,发现那个钝角不是很会处理,但是40分暴力应该还是可以写, ...
- 【COGS2652】秘术「天文密葬法」(长链剖分,分数规划)
[COGS2652]秘术「天文密葬法」(长链剖分,分数规划) 题面 Cogs 上面废话真多,建议直接拉到最下面看一句话题意吧: 给个树,第i个点有两个权值ai和bi,现在求一条长度为m的路径,使得Σa ...
- 「JLOI2015」战争调度 解题报告
「JLOI2015」战争调度 感觉一到晚上大脑就宕机了... 题目本身不难,就算没接触过想想也是可以想到的 这个满二叉树的深度很浅啊,每个点只会和它的\(n-1\)个祖先匹配啊 于是可以暴力枚举祖先链 ...
- luogu3292 幸运数字 (点分治+线性基)
首先第一眼是一个倍增套线性基,但是$O(Qlog^2Vlog^N)=10^{10}$的复杂度... 即使是st表也只是变成了$O(Nlog^2Vlog^N)$啊 考虑点分治,相对于倍增显著减少了线性基 ...
- bzoj1831 逆序对 (dp+树状数组)
注意到,所有的-1应该是一个不降的序列,否则不会更优那就先求出来不是-1的的逆序对个数,然后设f[i][j]表示第i个-1放成j的前i个-1带来的最小逆序对数量这个可以树状数组来求 #include& ...
- BZOJ3133[Baltic2013]ballmachine
题目描述 https://www.lydsy.com/JudgeOnline/problem.php?id=3133 题解 还是分两个操作来说吧. 先看第一个操作,放球,可以发现,对于祖先节点和后代节 ...
- 【php】php实现数组分块
有时候需要将一个大数组按一定大小分块,那么可以实现这个功能,代码如下: /** * @param array $arr * @param int $size <p> * @param bo ...
- poj 3252 Round Numbers(数位dp 处理前导零)
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
- 搭建james邮件服务器
把james解压到任何一个非中文无空格目录下: lib下添加必要的jar文件: 运行run.bat命令服务器,使用期间不要关闭. 创建邮件数据库 创建配置文件:james-database.prope ...