[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}'
相關文章
相關標籤/搜索