拓展:grep

193.  ref: https://blog.csdn.net/yanglingwell/article/details/82343407

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
grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$' file.txt
# . ^ 表示需要在行的开始处进行匹配. 例如, ^abc 可以匹配 abc123 但不匹配 123abc.
# . $ 表示需要在行的末端进行匹配. 例如, abc$ 可以匹配 123abc 但不能匹配 abc123.
# . \d可以匹配一个数字.'00\d'可以匹配'',但无法匹配'00A'.
# . {n}表示n个字符,用{n,m}表示n-m个字符. \d{}表示匹配3个数字,例如''.
# . -P(grep): Interpret the pattern as a Perl-compatible regular expression (PCRE).

195. ref: https://blog.csdn.net/sole_cc/article/details/44977821

Given a text file file.txt, print just the 10th line of the file.

Example:

Assume that file.txt has the following content:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10

Your script should output the tenth line, which is:

Line 10

Note:
1. If the file contains less than 10 lines, what should you output?
2. There's at least three different solutions. Try to explore all possibilities. 方法一:
awk NR== file.txt

//awk的默认动作就是打印$0,所以NR==10后面可以不用加{print $0}

方法二:

sed -n '10p' file.txt

//如果不够10行,则什么也不打印

bash leetcode的更多相关文章

  1. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

  2. bash 刷题leetcode

    题目一: 给定一个文本文件 file.txt,请只打印这个文件中的第十行. 示例: 假设 file.txt 有如下内容: Line 1 Line 2 Line 3 Line 4 Line 5 Line ...

  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 Bash练习

    195. Tenth Line #!/bin/bash i= cat file.txt | while read line do #echo $line ] then echo $line fi le ...

  5. 【leetcode】bash脚本练习

    [192]Word Frequency Write a bash script to calculate the frequency of each word in a text file words ...

  6. [LeetCode] 195. Tenth Line_Easy tag: Bash

    Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...

  7. [LeetCode] Tenth Line 第十行

    How would you print just the 10th line of a file? For example, assume that file.txt has the followin ...

  8. [LeetCode] Transpose File 转置文件

    Given a text file file.txt, transpose its content. You may assume that each row has the same number ...

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

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

随机推荐

  1. Python入门到进阶必看的权威书籍与网站

    随着人工智能全面爆发,Python[英文单词:蟒蛇],是一款近年来爆红的计算机编程语言.1989年发明,1991年发行,比目前应用最广的Java还要大7岁,有种大器晚成的感觉. 分享之前我还是要推荐下 ...

  2. python学习笔记(二)---for循环与操作列表

    内容概要 for循环 range(start,end,step)函数 生成随机数列表 list()函数 将range()的结果整合到某个列表 列表的操作 切片(start: end :step) 元组 ...

  3. php最快捷的插入数据,3000万仅需5秒

    <?phpheader('content-type:text/html;charset=utf-8');//采集数据$url="http://www.keepclub.com/club ...

  4. 正则表达式(grep,awk,sed)和通配符

    1. 正则表达式 1. 什么是正则表达式? 正则表达式就是为了处理大量的字符串而定义的一套规则和方法. 通过定义的这些特殊符号的辅助,系统管理员就可以快速过滤,替换或输出需要的字符串. Linux正则 ...

  5. 打造livecd的注意事项

    一:在CentOS.ks的定制脚本中,删除syslinux组件:出错提示: /usr/lib/python2.6/site-packages/imgcreate/errors.py:45: Depre ...

  6. 前端程序员难翻身,没有好的学习方法,你永远无法成功,vue.js专题

    学习vue正确思路,是先学vue-cli,再学vue.js单文件引用的用法,这样会在极短时间内撤底撑握vue, 如果先学vue.js单文件用法,再去学vue-cli4,可以说是重新学vue,,,,难处 ...

  7. Floyd —Warshall(最短路及其他用法详解)

    一.多元最短路求法 多元都求出来了,单源的肯定也能求. 思想是动态规划的思想:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设Dis(A ...

  8. 2) 接口规范 原生django接口、单查群查 postman工具 CBV源码解析

    内容了解 """ .接口:什么是接口.restful接口规范 .CBV生命周期源码 - 基于restful规范下的CBV接口 .请求组件.解析组件.响应组件 .序列化组件 ...

  9. centos7 安装高版本svn

    一.安装高版本svn 1.创建一个新的yum库文件,vim /etc/yum.repos.d/wandisco-svn.repo 内容如下 [WandiscoSVN] name=Wandisco SV ...

  10. Java中的动态定义数组

    1.一维矩阵的动态定义(代码注释) 1.1方法一 package dongtai; import java.util.Scanner; import java.util.ArrayList; publ ...