[Leetcode] Tenth Line的笔记

来源:互联网 发布:淘宝联盟活动推广 编辑:程序博客网 时间:2024/06/05 20:55

发现leetcode上面有shell脚本和sql的题目,那么就做完他们来巩固一下学过的知识。


题目如下

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


网上有各种各样的思路和解决办法,我先share一下我自己的方法1

  • Solution1: 思路就是删除前面九个答案,然后显示最头上面的一个内容。
sed '1,9d' < file.txt| head -1
  • Solution2: 就显示第十个参数
awk 'NR == 10' file.txt
  • Solution3: 通过sed的n参数表示只打印需要显示的
sed -n 10p file.txt

当然还有其他的方法,循环,tail等命令都是可以的,在此记录,慢慢积累。

0 0
原创粉丝点击