拓展: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. CG-CTF(4)

    CG-CTF https://cgctf.nuptsast.com/challenges#Web 续上~ 第十六题:bypass again 代码分析: 当a不等于b,且a和b的md5值相同时,才会返 ...

  2. 2019-2020-1 20199328《Linux内核原理与分析》第九周作业

    笔记部分 2019/11/12 14:45:44 从CPU和内存的角度看linux系统的运行 CPU角度:首先我们进行了系统调度,然后系统进入内核态,把信息压栈,然后我们进行进程管理,由于进入系统调用 ...

  3. 13个Python图形库

    By Django中国社区 at 2013-04-27 07:49 Python的13大图形库,matplotlib功能最强大,Cairoplot最漂亮,django-chartit与Django集成 ...

  4. 【Linux网络基础】TCP/IP 协议簇(各个常见协议介绍)

    一.应用层协议 1. FTP   协议所在层次:应用层协议 名称:FTP协议 协议端口:20,21 协议说明: FTP(File Transfer Protocol,文件传输协议)是TCP/IP协议组 ...

  5. Codeforces Round #561 (Div. 2) A. Silent Classroom(贪心)

    A. Silent Classroom time limit per test1 second memory limit per test256 megabytes inputstandard inp ...

  6. 图论--2-SAT--详解

    问题描述: 现有一个由N个布尔值组成的序列A,给出一些限制关系,比如A[x]AND A[y]=0.A[x] OR A[y] OR A[z]=1等,要确定A[0..N-1]的值,使得其满足所有限制关系. ...

  7. muduo网络库源码学习————Exception类

    Exception类是为异常捕获而设计,可以获得异常的信息以及栈的回溯信息 (原来的代码没有demangle成员函数,输出的格式比较难看,加了demangle成员函数,利用demangle成员函数可以 ...

  8. 老师,你确定Java注释不会被执行吗?

    之前在博客上分享过一篇文章,涉及到 Java 中的注释,就信誓旦旦地写了一句话:"注释是不会被执行的!"结果,有小伙伴留言说,"老师,你确定吗?" 我这个人一直 ...

  9. jquery 根据后端传过来的value值,让小原点根据不同值区间去左右定位

    /** * 小圆点滑动 * @param {目标dom} barEle * @param {最小值} min * @param {最大值} max * @param {dom总长度} domLang ...

  10. IP 基础知识全家桶,45 张图一套带走

    前言 前段时间,有读者希望我写一篇关于 IP 分类地址.子网划分等的文章,他反馈常常混淆,摸不着头脑. 那么,说来就来!而且要盘就盘全一点,顺便挑战下小林的图解功力,所以就来个 IP 基础知识全家桶. ...