bash leetcode
拓展: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的更多相关文章
- [LeetCode] All questions numbers conclusion 所有题目题号
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...
- bash 刷题leetcode
题目一: 给定一个文本文件 file.txt,请只打印这个文件中的第十行. 示例: 假设 file.txt 有如下内容: Line 1 Line 2 Line 3 Line 4 Line 5 Line ...
- [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 Bash练习
195. Tenth Line #!/bin/bash i= cat file.txt | while read line do #echo $line ] then echo $line fi le ...
- 【leetcode】bash脚本练习
[192]Word Frequency Write a bash script to calculate the frequency of each word in a text file words ...
- [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 ...
- [LeetCode] Tenth Line 第十行
How would you print just the 10th line of a file? For example, assume that file.txt has the followin ...
- [LeetCode] Transpose File 转置文件
Given a text file file.txt, transpose its content. You may assume that each row has the same number ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
随机推荐
- Python模块---制作新冠疫情世界地图()
目录 pyecharts模块 简介 安装pyecharts 测试pyecharts模块 pyecharts实战:绘制新冠肺炎疫情地图 需求分析 请求数据 提取数据 处理数据 制作可视化地图 设置可视化 ...
- [go]包和工程管理
一.系统环境变量 GOROOT 指定go的安装目录,win是在 C\Go\,Linux在 /usr/local/go下,如果不是默认的目录,则需要指定 GOROOT环境变量,否则不需要 GOPATH ...
- PHP 获取前两页的url地址
通过隐藏表单控件 <input type="hidden" name="prevurl" value="<?php echo $_SERV ...
- CentOS上安装配置 mongodb
CentOS 首先yum list mongo* 查看是否有关于mongo的安装包,检查后安装即可 mongo 分client端和server端,server启动db服务,client可以连接到s ...
- 如何高效使用vim
Vim 是一款文本编辑器,被称为编辑器之神,非常适合在shell 中编辑代码,熟练的使用Vim,可以让你高效的编写代码. Vim 是Vi 的增强版,所有的类Unix 系统,都自带这两个工具,这两个工具 ...
- CF1324 --- Maximum White Subtree
CF1324 --- Maximum White Subtree 题干 You are given a tree consisting of \(n\) vertices. A tree is a c ...
- CSS样式2
1.css重用 <style> .cl{ ... } .c2{ ... } .c{ ... } </style> <div class='c c2'></di ...
- Cobbler自动装机试验
Cobbler自动装机简介:Cobbler是一个使用Python开发的开源项目,通过将部署系统所涉及的所有服务集中在一起,来提供一个全自动的批量快速建立Linux系统的网络安装环境.Cobbler提供 ...
- AWS访问慢的原因分析及解决方案
中国区的用户在访问海外AWS服务器的时候会遇到访问很慢的情况,那如何快速访问海外AWS服务器,今天和大家一起聊一下这个话题. 首先,为什么中国的用户访问海外AWS会变慢? 我总结来下大概有以下几方面的 ...
- strip()的正则表达式版本
题目:写一个函数,它接受一个字符串,做的事情和 strip()字符串方法一样.如果只 传入了要去除的字符串,没有其他参数,那么就从该字符串首尾去除空白字符. 否则,函数第二个参数指定的字符将从该字符串 ...