[Bash]LeetCode195. 第十行 | Tenth Line

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-qvqzdklu-md.html 
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a text file file.txt, print just the 10th line of the file.git

Example:github

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:less

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,请只打印这个文件中的第十行。spa

示例:code

假设 file.txt 有以下内容:htm

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

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

Line 10

说明:
1. 若是文件少于十行,你应当输出什么?
2. 至少有三种不一样的解法,请尝试尽量多的方法来解题。three


 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}'
相关文章
相关标签/搜索