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 

原文地址:https://www.cnblogs.com/douzujun/p/10416060.html

时间: 2024-10-20 06:14:16

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

Leetcode 193.Valid Phone Numbers

https://leetcode.com/problems/valid-phone-numbers/description/Shell脚本题目:给file.txt ,输出复合格式的电话号码.思路是使用正则,但是有几个坑. 注意grep -E 与grep -P 的区别 https://www.jianshu.com/p/e1acfb7989b2-E 其实是扩展支持| 与& 这种方式,-P才是Perl正则 注意正则使用^ 和 $ 包围精确匹配 刚开始使用cat 和read 读取文件,发现要么多个空格

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

[LeetCode] 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

[LeetCode] 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

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

leetcode -day13 Valid Palindrome & Triangle & Pascal's Triangle I II

1.  Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note:

LeetCode --- 2. Add Two Numbers

题目链接:Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

linux之sed用法

sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为:         sed [-nefri] 'command' 输入文本 常用选项:        -n∶使用安静(silent)模式.在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到萤幕上.但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来.        -e∶直接在