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. 题解 洛谷P1236 【算24点】

    不得不说,个人认为许多大佬们把程序想复杂了,所以码量很长,但是实际上这题并不要这么复杂... 可以考虑用一个\(dfs\)维护一个状态\(f(n)[a_1,a_2--a_n]\) 接下来我们暴力枚举两 ...

  2. background-image:url为空引发的两次请求问题

    参考文章: https://blog.csdn.net/jsjhushilei/article/details/51101014 1.Nicholas 在 2009 年就开始推动各浏览器厂商,现在看起 ...

  3. GITHUB常见命令

    1 常用 $ git remote add origin git@github.com:yeszao/dofiler.git # 配置远程git版本库 $ git pull origin master ...

  4. go 学习 (一):环境配置

    Go 下载地址:https://golang.google.cn/dl/ 右键我的电脑  --> 左上方 “高级系统设置”   ---> 环境变量  -->  第二个菜单栏 “系统变 ...

  5. K-Nearest Neighbors Algorithm

    K近邻算法. KNN算法非常简单,非常有效.KNN算法适合样本较少典型性较好的样本集. KNN的模型表示是整个训练数据集.也可以说是:KNN的特点是完全跟着数据走,没有数学模型可言. 对一个新的数据点 ...

  6. LOJ P10015 扩散 题解

    每日一题 day49 打卡 Analysis 用dis数组记录每两个点之间的时间,再用一个传递闭包来维护最小的时间就好了 #include<iostream> #include<cs ...

  7. oracle的一些状态查询

  8. Nodejs中的路径问题

    一.path核心模块 ①path.basename(path[,ext])获取一个路径中的文件名 var path=require('path'); console.log(path.basename ...

  9. nbbnbnbnbnb

    1.本章学习总结(2分) 1.1 学习内容总结 结构体的定义与赋值 结构类型定义的一般形式为: struct 结构名 { 类型名 结构成员1: 类型名 结构成员2: ... 类型名 结构成员3: }; ...

  10. struct iphdr

    struct iphdr { #if defined(__LITTLE_ENDIAN_BITFIELD) __u8 ihl:, version:; #elif defined (__BIG_ENDIA ...