为有牺牲多壮志,敢教日月换新天。

[Bash]LeetCode195. 第十行 | Tenth Line

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10180443.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

给定一个文本文件 file.txt,请只打印这个文件中的第十行。

示例:

假设 file.txt 有如下内容:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10

你的脚本应当显示第十行:

Line 10

说明:
1. 如果文件少于十行,你应当输出什么?
2. 至少有三种不同的解法,请尝试尽可能多的方法来解题。


 4ms

1 # Read from the file file.txt and output the tenth line to stdout.
2 sed -n 10p file.txt

4ms

1 # Read from the file file.txt and output the tenth line to stdout.
2 awk 'NR==10' file.txt

8ms

1 # Read from the file file.txt and output the tenth line to stdout.
2 { cat file.txt; for (( i = 1; i <= 10; ++i)); do echo ; done } | head -n 10 | tail -n 1

12ms

1 # Read from the file file.txt and output the tenth line to stdout.
2 
3 sed 's/\\n/\
4 /g' file.txt | awk 'NR==10{print}'

 

posted @ 2018-12-26 17:28  为敢技术  阅读(429)  评论(0编辑  收藏  举报