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 ...
随机推荐
- (一)PL/SQL简介
PL/SQL PL/SQL也是一种程序语言,叫做过程化SQL语言(Procedural Language/SQL).PL/SQL是Oracle数据库对SQL语句的扩展.在普通SQL语句的使用上增加了编 ...
- ansible的role(6)
roles的优点 roles能够根据层次型结构自动的装载变化量文件,task,以及handles等. 要使用肉了是只需要在playbook中使用include引入即可. 主要使用场景复制代码较高的情况 ...
- 播放声音 (c++) (windows)
自己看自己看自己看自己看自己看自己看 在<windows.h>中 一:BOOL WINAPI MessageBeep (_in UINT uType ); 播放一个波形文件 (也就是wac ...
- JAVA 之 EL表达式
一.什么是EL表达式 1.Expression Language表达式语言 2.是一种在JSP页面获取数据的简单方式(只能获取数据,不能设置数据) 语法格式:${expression } 二.EL中的 ...
- jQuery dataTables四种数据来源[转]
2019独角兽企业重金招聘Python工程师标准>>> 四种数据来源 对于 dataTables 来说,支持四种表格数据来源. 最为基本的就是来源于网页,网页被浏览器解析为 DOM ...
- Eclipse Mac OS 安装 最新版 Subversion插件subclipse
subclipse 目前全部转移到github 官方地址 https://github.com/subclipse/subclipse/wiki Eclipse mac版 安装 最新版svn插件sub ...
- Facebook发布神经蛋分离法,可从嘈杂环境中提取音视频
分离混合分布是机器学习和信号处理的长期挑战,而Facebook近日提出的新方法似乎可以有效解决这一难题. 人类天生善于分离个别声音和视觉效果,例如在拥挤的鸡尾酒会上听到别人的声音,或者在动物穿过灌木丛 ...
- tomcat项目迁移,无法访问,报“404”错误,原因分析
首先,导出项目文件和MySQL数据库(项目文件直接tar&&cp:数据库直接mysqldump生成sql文件) 再,进行导入步骤,项目文件拷贝到webapps下,并赋予bin相关文件执 ...
- 2016年全球IC设计大厂营收排名:高通稳居龙头
TrendForce旗下拓墣产业研究所最新研究统计,2016年全球前十大无晶圆IC设计业者营收中,高通(QCT)仍然稳居龙头宝座.而前三大业者高通.新博通(Broadcom)与联发科合计营收占前十名营 ...
- centos 7.0运行docker出现内核报错解决方法
目前我这里docker是运行在centos 7.0系统里,使用1.5版本docker,最近一台服务器总是不定期死机,通过查看日志发现属于内核bug导致,报错信息如下 1 2 3 4 5 6 7 8 9 ...