[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 ...
随机推荐
- MySQL:查询、修改(二)
干货: 使用SELECT查询的基本语句SELECT * FROM <表名>可以查询一个表的所有行和所有列的数据.SELECT查询的结果是一个二维表. 使用SELECT *表示查询表的所有列 ...
- mac随笔
1.查看端口,lsof -i tcp:post lsof -i tcp:8086 2.kill进程 找到进程的PID,使用kill命令:kill PID(进程的PID,如2044),杀死对应的进程 k ...
- python开发笔记-类
类的基本概念: 问题空间:问题空间是问题解决者对一个问题所达到的全部认识状态,它是由问题解决者利用问题所包含的信息和已贮存的信息主动的地构成的. 初始状态:一开始时的不完全的信息或令人不满意的状况: ...
- 浏览器报400-Bad Request异常
今天在使用ie浏览器在测试程序的时候,报这个错误,后台日志打印出来显示的是:连接一个远程主机失败 解决Invalid character found in the request target. Th ...
- 六.深浅copy
先问问大家,什么是拷贝?拷贝是音译的词,其实他是从copy这个英文单词音译过来的,那什么是copy? copy其实就是复制一份,也就是所谓的抄一份.深浅copy其实就是完全复制一份,和部分复制一份的意 ...
- 【NOIP2017】逛公园 D1 T3
记忆化搜索 跑一次反向的最短路求出MinDis(u,n)MinDis(u,n)MinDis(u,n) f[u][k]f[u][k]f[u][k]表示dis(u,n)<=MinDis(u,n)+d ...
- Java web开发——文件夹的上传和下载
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...
- WinDbg常用命令系列---!teb
!teb 简介 !teb扩展显示线程环境块(teb)中信息的格式化视图. 使用形式 !teb [TEB-Address] 参数 TEB-Address 要检查其TEB的线程的十六进制地址.(这不是从线 ...
- pgloader 学习(七) 从归档文件加载数据
我们可以直接从zip,tar,gzip 文件获取内容 command file 参考格式 LOAD ARCHIVE FROM /Users/dim/Downloads/GeoLiteCity-late ...
- JMeter学习2
JMeter学习(四)参数化 参数化:录制脚本中有登录操作,需要输入用户名和密码,假如系统不允许相同的用户名和密码同时登录,或者想更好的模拟多个用户来登录系统. 这个时候就需要对用户名和密码进行参数化 ...