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.

用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 第十行的更多相关文章

  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] Tenth Line 第十行

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

  3. 195 Tenth Line

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

  4. [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 ...

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

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

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

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

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

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

  8. Tenth Line

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

  9. 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 ...

随机推荐

  1. Kotlin属性委托系统总结与提供委托详解

    属性委托总结回顾: 在前三次已经将Kotlin委托相关的知识点进行了完整的学习了,具体博文如下: https://www.cnblogs.com/webor2006/p/11369019.html h ...

  2. Windows环境下安装和使用nginx1.16.0

    nginx是一款开源的HTTP服务器和反向代理服务器,nginx可以作为Web服务器提供HTTP访问功能,类似于Apache.IIS等.目前nginx已经在国内外很多网站作为Web服务器或反向代理服务 ...

  3. 对于模块加载:ES6、CommonJS、AMD、CMD的区别

    运行和编译的概念 编译包括编译和链接两步. 编译,把源代码翻译成机器能识别的代码或者某个中间状态的语言. 比如java只有JVM识别的字节码,C#中只有CLR能识别的MSIL.还简单的作一些比如检查有 ...

  4. java 修改HttpServletRequest的参数或请求头

    场景:过滤器中获取参数Token并添加到请求头(用户认证兼容老系统) 请求头和请求参数是不能直接修改,也没有提供修改的方法,但是可以在过滤器和拦截器中使用HttpServletRequestWrapp ...

  5. learning java Charset 查看支持的字符集类型

    import java.nio.charset.Charset; import java.util.SortedMap; public class CharsetTest { public stati ...

  6. CSS块元素

    一.典型代表: Div h1-h6 p ul li 二.特点: 独占一行 可以设置宽高 嵌套(包含)下,子块元素宽度(没有定义情况下)和父块元素宽度默认一致. <style type=" ...

  7. [RN] React Native 下实现底部标签(支持滑动切换)

    上一篇文章 [RN] React Native 下实现底部标签(不支持滑动切换) 总结了不支持滑动切换的方法,此篇文章总结出 支持滑动 的方法 准备工作之类的,跟上文类似,大家可点击上文查看相关内容. ...

  8. cyyz: Day 6 平衡树整理

    一.平衡树 知识点: ,并且左右两个子树都是一棵平衡二叉树.平衡二叉树的常用实现方法有红黑树.AVL.替罪羊树.Treap.伸展树等. 最小二叉平衡树的节点的公式如下 F(n)=F(n-1)+F(n- ...

  9. 洛谷 P2563 [AHOI2001]质数和分解 题解

    P2563 [AHOI2001]质数和分解 题目描述 任何大于 1 的自然数 n 都可以写成若干个大于等于 2 且小于等于 n 的质数之和表达式(包括只有一个数构成的和表达式的情况),并且可能有不止一 ...

  10. 和jz姐姐的vp记录

    即使如此,jz姐姐也漂亮的取得了胜利 有些懒得写直接口胡,所以代码也不一定有 暂时停更了 2015-2016 Petrozavodsk Winter Training Camp, Makoto rng ...