列印第幾行內容 shell

2021-08-22 15:20:13 字數 1216 閱讀 4167

hldemacbook-air:~ hl$ more file.txt 

line 1

line 2

line 3

line 4

line 5

line 6

line 7

line 8

line 9

line 10

hldemacbook-air:~ hl$

檔案file.txt 內容如上,列印第9行內容,可以採取以下幾種方式

tail -n +9 filename     從第9行輸出

head -n 1 filename    輸出前1行

hldemacbook-air:~ hl$ tail -n +9 file.txt 

line 9

line 10

hldemacbook-air:~ hl$

hldemacbook-air:~ hl$ tail -n +9 file.txt | head -n 1

line 9

hldemacbook-air:~ hl$

ps:不要寫成  head -n count file.txt | tail -n 1 ;   因為當file.txt 沒有count行,按這個方式就會變顯示最後一行的內容

hldemacbook-air:~ hl$ sed -n '9p' file.txt 

line 9

hldemacbook-air:~ hl$ sed -n '9,10p' file.txt

line 9

line 10

hldemacbook-air:~ hl$

'9,10p' : 第9~10行

nr     ordinal number of the current record

fnr ordinal number of the current record in the current file

hldemacbook-air:~ hl$ awk nr==9 file.txt file.txt

line 9

hldemacbook-air:~ hl$ awk fnr==9 file.txt file.txt

line 9

line 9

hldemacbook-air:~ hl$

MySql第幾行到第幾行語句

1 查詢第一行記錄 select from table limit 1 2 查詢第n行到第m行記錄 select from table1 limit n 1,m n select from table limit 5,10 返回第6行到第15行的記錄 select from employee lim...

mysql查詢第幾行到第幾行記錄

1 查詢第一行記錄 select from table limit 1 2 查詢第n行到第m行記錄 select from table1 limit n 1,m n select from table limit 5,10 返回第6行到第15行的記錄 select from employee lim...

Linux 檢視檔案行數,第幾行至第幾行

進入檔案 vi test.txt 顯示行號 為shift 進入命令模式 set nu 跳轉到第幾行 20 跳轉到20行 跳轉後游標顯示在20行 擷取檢視第5行到第6行 寫法1 cat test.txt n tail n 5 head n 2 寫法2 cat test.txt n head n 6 t...