LeetCode195——第十行

版权声明:我的GitHub:https://github.com/617076674。真诚求星! https://blog.csdn.net/qq_41231926/article/details/86636833

我的LeetCode代码仓:https://github.com/617076674/LeetCode

原题链接:https://leetcode-cn.com/problems/tenth-line/description/

题目描述:

知识点:Linux常用指令

思路一:while循环记录行号

Bash脚本:

cnt=0
while read line && [ $cnt -le 10 ] 
do
  let cnt++
  if [ $cnt -eq 10 ] 
  then
    echo $line
    exit 0
  fi
done < file.txt

LeetCode解题报告:

思路二:awk命令

Bash脚本:

awk 'NR == 10' file.txt

LeetCode解题报告:

思路三:sed命令

-n选项:取消默认的输出,使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到屏幕上。但如果加上-n后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。

p:列印,亦即将某个选择的资料印出。通常 p 会与参数sed -n一起用。

Bash脚本:

sed -n 10p file.txt

LeetCode解题报告:

思路四:tail命令

tail命令和head命令的-n选项均用来指定显示的行数。

Bash脚本:

tail -n +10 file.txt | head -n +1

LeetCode解题报告:

猜你喜欢

转载自blog.csdn.net/qq_41231926/article/details/86636833