[LeetCode] 195. Tenth Line 第十行
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
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.
用Bash脚本来打印一个txt文件的第十行。
1. awk是强大的文本分析工具,具有流控制、数学运算、进程控制、内置的变量和函数、循环和判断的功能。其中NR表示行数,$0表示当前记录
awk '{if(NR == 10) print $0}' file.txt
# OR
awk 'FNR == 10 {print }' file.txt
# OR
awk 'NR == 10' file.txt
2. 使用流编辑工具sed来做。-n默认表示打印所有行,p限定了具体打印的行数
sed -n 10p file.txt
3. 使用tail和head关键字来打印。head表示从头开始打印,tail表示从结尾开始打印,'-' 表示根据文件行数进行打印。例如:
tail -n 3 file.txt: 打印file文件的最后三行内容
tail -n +3 file.txt: 从file文件第三行开始打印所有内容
head -n 3 file.txt: 打印file文件的前三行
head -n -3 file.txt: 打印file文件除了最后三行的所有内容
竖杠|为管道命令,用法: command 1 | command 2 , 把第一个命令command1执行的结果作为command 2的输入传给command 2。
tail -n +10 file.txt | head -n 1
or
head -n 10 file.txt | tail -n +10
4. 编写函数
cnt=0
while read line && [ $cnt -le 10 ]; do
let 'cnt = cnt + 1'
if [ $cnt -eq 10 ]; then
echo $line
exit 0
fi
done < file.txt
All LeetCode Questions List 题目汇总
[LeetCode] 195. Tenth Line 第十行的更多相关文章
- Leetcode 195 Tenth Line
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 ...
- 195 Tenth Line
Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...
- [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 OJ:Tenth Line(文件第十行)
How would you print just the 10th line of a file? For example, assume that file.txt has the followin ...
- [Bash]LeetCode195. 第十行 | Tenth Line
Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...
- 刷题中熟悉Shell命令之Tenth Line和Transpose File [leetcode]
首先介绍题目中要用的4个Shell命令 sed awk head tail的常用方法.(打好地基,才能建成高楼!) sed:(转自:http://www.cnblogs.com/barrychiao/ ...
- Tenth Line
How would you print just the 10th line of a file? For example, assume that file.txt has the followin ...
- LeetCode 562. Longest Line of Consecutive One in Matrix(在矩阵中最长的连续1)$
Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horiz ...
随机推荐
- 动态生成16位不重复随机数、随机创建2位ID
/** 1. * 动态生成16位不重复随机数 * * @return */ public synchronized static String generate16() { StringBuffer ...
- windows——快速得到某一目录下所有文件的名称
前言 其实用的是windows上dir命令,能快速得到某一目录下的所有文件名称,天天那么忙都没时间写博客(┬_┬) 步骤 打开cmd并cd到某目录下 C:\Users\Administrator.KI ...
- MAT022 Foundations of Statistics
MAT022 Foundations of Statistics and Data Science Summative Assessment 2019/20MAT022 Foundations of ...
- springboot项目报错Could not resolve placeholder 'datasource.type' in value "${datasource.type}"解决办法
一,首先确认数据库的连接信息是否都正确,数据库能否正常连接(例如用客户端能连接上):二,确认配置文件中datasource.type配置是否正确,此处我们公司用的阿里的是com.alibaba.dru ...
- 了解一下Elasticsearch的基本概念
一.前文介绍 Elasticsearch(简称ES)是一个基于Apache Lucene(TM)的开源搜索引擎,无论在开源还是专有领域,Lucene 可以被认为是迄今为止最先进.性能最好的.功能最全的 ...
- Python微信操控(itchat)
itchat是一个开源的微信个人号接口,使用python调用微信从未如此简单. 开源地址 https://github.com/littlecodersh/ItChat 文档: https://itc ...
- for循环计算
计算0-100之间所有偶数的和: var a = 0 ; //声明一个变量 for (var i = 0; i<100 ; i++){ //起始条件 判断条件 结束条件 if (i%2===0) ...
- Tomcat启动服务报错:Unknown version string [4.0]. Default version will be used.
Tomcat.jdk.web.xml 对应关系: 版本对应错误,更换便可.(版本往下兼容) web.xml——version2.2——JDK1.1——Tomcat3.3 web.xml——versio ...
- 第03组 团队git现场编程实战
1.组员职责分工 张逸杰:复制监督整个编程任务的进程以及协助组员编程 黄智锋.刘汪洋:负责UI设计 苏凯婷.鲍冰如:爬取数据并负责测评出福州最受欢迎的商圈 陈荣杰.杨锦镔:爬取数据并负责测评出福州人均 ...
- Java-Long类型精度丢失问题
问题 今天碰到一个问题,后端需要返回给前端Long类型的id,前端收到的id会发生精度丢失. 测试代码:后端返回的值为344739147160346624 但是前端获取的值为: 解决办法 将返回的值转 ...