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. 微信小程序~TabBar底部导航切换栏

    底部导航栏这个功能是非常常见的一个功能,基本上一个完成的app,都会存在一个导航栏,那么微信小程序的导航栏该怎么实现呢?经过无数的踩坑,终于实现了,好了,先看看效果图. 对于底部导航栏,小程序上给出的 ...

  2. Spring源码窥探之:单实例Bean的创建过程

    finishBeanFactoryInitialization(beanFactory);初始化剩下的所有的单实例(非懒加载)Bean(Instantiate all remaining (non-l ...

  3. Laravel —— batch 实现

    很多项目中会用到自动执行脚本的功能, 例如,自动统计上个月的注册用户,定时生成 csv 文件并邮箱发送给客户等等. Laravel 中的任务调度,可实现定时任务, 结合自定义 artisan 命令,即 ...

  4. 安装tomacat之后出现需要授权窗口

    安装tomacat之后登录网页,出现需要授权窗口,输入正确密码后依然会循环弹出该窗口. 原因: 因为tomcat的端口与oracle的端口重复了. 解决: 进入任务管理器,然后将oracle运行的程序 ...

  5. nginx 配置文件正确性测试

    今日思语:每天都要不一样,那么每天就应该多学习 在安装完nginx之后,我们可以使用nginx的测试命令来验证下nginx.conf的配置是否正确: 方式一:不指定文件 nginx -t 如上可知/e ...

  6. Stability Analysis of Algorithms

    算法(Algorithm)是指用来操作数据.解决程序问题的一组方法.对于同一个问题,使用不同的算法,也许最终得到的结果是一样的,比如排序就有前面的十大经典排序和几种奇葩排序,虽然结果相同,但在过程中消 ...

  7. shell脚本sed的基本用法

    sed 我们首先准备了一个测试文件 1. s 替换  将文件中的This替换cyy 在替换的时候如果加入了 -i 选项就会真的替换,但是只会替换每一行的第一个 -n 和 -p 一起使用表示的是打印那些 ...

  8. 用TortoiseSVN从github下载单个文件

    问题描述: github是一个很好的共享代码管理仓库,我们可以从github上直接以压缩包的形式直接download整个项目,也可以通过git,用git clone + URL 命令下载整个目录. 但 ...

  9. S1_搭建分布式OpenStack集群_09 cinder 控制节点配置

    一.创建数据库创建数据库以及用户:# mysql -uroot -p12345678MariaDB [(none)]> CREATE DATABASE cinder;MariaDB [(none ...

  10. AnsiString

    原文链接:https://blog.csdn.net/Li_Ning_/article/details/82981092 /* * 编号:Number 1 * 函数:substring * 说明:截取 ...