[Leetcode] Tenth Line的筆記

發現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等命令都是可以的,在此記錄,慢慢積累。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章