How would you print just the 10th line of a file?

For 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

[show hint]

Hint:
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文件的第十行,可以用很多种方法来实现,我们先来看一种是用awk来实现的方法,awk是强大的文本分析工具,具有流控制、数学运算、进程控制、内置的变量和函数、循环和判断的功能,具体可以参见这个帖子。其中NR表示行数,$0表示当前记录,所以我们可以用if来判断行数为第十行时,将内容打印出来即可:

解法一:

awk '{if(NR == 10) print $0}' file.txt

我们也可以用更简洁的写法来打印出第十行如下:

解法二:

awk 'NR == 10' file.txt

我们也可以使用流编辑工具sed来做,关于sed的讲解可以参见这个帖子。-n默认表示打印所有行,p限定了具体打印的行数:

解法三:

sed -n 10p file.txt

我们也可以使用tail和head关键字来打印,关于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。了解了这些知识,那么下面一行命令就很好理解了,首先输入file文件从第十行开始的所有内容,然后将输出内容的第一行打印出来即为第十行:

解法四:

tail -n + file.txt | head -n 

下面这种方法跟上面刚好相反,先输出file文件的前十行,然后从输出的第十行开始打印,那么也能正好打印第十行的内容:

解法五:

head -n  file.txt | tail -n +

参考资料:

https://leetcode.com/discuss/29526/super-simple-solution

https://leetcode.com/discuss/29591/simple-solution-using-awk

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Tenth Line 第十行的更多相关文章

  1. [LeetCode] 195. Tenth Line 第十行

    Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...

  2. LeetCode OJ:Tenth Line(文件第十行)

    How would you print just the 10th line of a file? For example, assume that file.txt has the followin ...

  3. [Bash]LeetCode195. 第十行 | Tenth Line

    Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...

  4. 刷题中熟悉Shell命令之Tenth Line和Transpose File [leetcode]

    首先介绍题目中要用的4个Shell命令 sed awk head tail的常用方法.(打好地基,才能建成高楼!) sed:(转自:http://www.cnblogs.com/barrychiao/ ...

  5. Leetcode 195 Tenth Line

    Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...

  6. Tenth Line

    How would you print just the 10th line of a file? For example, assume that file.txt has the followin ...

  7. 195 Tenth Line

    Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...

  8. [LeetCode] 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 ...

  9. LeetCode Shell Problems

    195. Tenth Line -- 第十行 How would you print just the 10th line of a file? Solution: awk 'NR==10' file ...

随机推荐

  1. Solr5.5.1 IK中文分词配置与使用

    前言 用过Lucene.net的都知道,我们自己搭建索引服务器时和解决搜索匹配度的问题都用到过盘古分词.其中包含一个词典. 那么既然用到了这种国际化的框架,那么就避免不了中文分词.尤其是国内特殊行业比 ...

  2. 不得不吐槽的Android PopupWindow的几个痛点(实现带箭头的上下文菜单遇到的坑)

    说到PopupWindow,我个人感觉是又爱又恨,没有深入使用之前总觉得这个东西应该很简单,很好用,但是真正使用PopupWindow实现一些效果的时候总会遇到一些问题,但是即便是人家的api有问题, ...

  3. C#/ASP.NET完善的DBHelper,配套Model生成器

    支持Oracle.MSSQL.MySQL.SQLite四种数据库,支持事务,支持对象关系映射:已在多个项目中实际使用. 没有语法糖,学习成本几乎为0,拿来即用. DBHelper类完整代码: usin ...

  4. 回顾java基础—Java数据类型

    1  java数据类型.8种基本类型,3种引用类型 基本类型(也称:原始类型):byte.short.int.long.char.float.double.boolean 引用类型:类,接口,数组 2 ...

  5. 【特种兵系列】String中的==和equals()

    1. 小样示例 public static void main(String[] args) { String a = "a" + "b" + 123; Str ...

  6. 学习廖雪峰的git教程

    地址:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 1.git add:添加文件 ...

  7. iOS 3D 之 SceneKit框架Demo分析

    Scene Kit 是Apple 向 OS X 开发者们提供的 Cocoa 下的 3D 渲染框架. Scene Kit 建立在 OpenGL 的基础上,包含了如光照.模型.材质.摄像机等高级引擎特性, ...

  8. 长按TextField或TextView显示中文的粘贴复制

    首先要确保手机当前系统为中文,只需要在 plist 文件中添加 Localized resources can be mixed = YES 就行了

  9. Android requires compiler compliance level 5.0 or 6.0. Found '1.4' instead的解决办法

    今天在导入工程进Eclipse的时候竟然出错了,控制台输出的是: [2013-02-04 22:17:13 - takepicture] Android requires compiler compl ...

  10. 20-C语言结束

    我回去看了下5号写的.虽然今天已经21号了~,花了16天. 复习完C专题,接下来我大概会用C做一些操作系统/信息安全的小东西,会发到博客里,敬请期待! ----2016/11/21