本文翻譯自:Going to a specific line number using Less in Unix
I have a file that has around million lines. 我有一個大約有一百萬行的文件。 I need to go to line number 320123 to check the data. 我需要轉到第320123行來檢查數據。 How do I do that? 我怎麼做?
#1樓
參考:https://stackoom.com/question/a1mK/使用Unix中的Less來轉到特定的行號
#2樓
For editing this is possible in nano
via +n
from command line, eg, 對於編輯,這可以在命令行的nano
via +n
進行,例如,
nano +16 file.txt
To open file.txt
to line 16. 將file.txt
打開到第16行。
#3樓
To open at a specific line straight from the command line, use: 要直接從命令行打開特定行,請使用:
less +320123 filename
If you want to see the line numbers too: 如果你想看到行號:
less +320123 -N filename
You can also choose to display a specific line of the file at a specific line of the terminal, for when you need a few lines of context. 您還可以選擇在終端的特定行顯示文件的特定行,以便在需要幾行上下文時顯示。 For example, this will open the file with line 320123 on the 10th line of the terminal: 例如,這將在終端的第10行打開帶有320123行的文件:
less +320123 -j 10 filename
#4樓
From within less (in Linux): 從少到少(在Linux中):
g and the line number to go forward
G and the line number to go backwards
Used alone, g and G will take you to the first and last line in a file respectively; 單獨使用,g和G將分別帶到文件的第一行和最後一行; used with a number they are both equivalent. 與數字一起使用它們都是等價的。
An example; 一個例子; you want to go to line 320123 of a file, 你想去一個文件的第320123行,
press 'g' and after the colon type in the number 320123 按'g'並在冒號後輸入數字320123
Additionally you can type '-N' inside less to activate / deactivate the line numbers. 此外,您可以在less內鍵入'-N'來激活/停用行號。 You can as a matter of fact pass any command line switches from inside the program, such as -j or -N. 事實上,您可以從程序內部傳遞任何命令行開關,例如-j或-N。
NOTE: You can provide the line number in the command line to start less (less +number -N) which will be much faster than doing it from inside the program: 注意:您可以在命令行中提供行號以減少(少於+數字-N),這比在程序內部執行要快得多:
less +12345 -N /var/log/hugelogfile
This will open a file displaying the line numbers and starting at line 12345 這將打開一個顯示行號的文件,從第12345行開始
Source: man 1 less and built-in help in less (less 418) 資料來源:男子少1人,內置幫助少(少418人)
#5樓
With n
being the line number: n
爲行號:
-
ng
: Jump to line number n.ng
:跳轉到第n行。 Default is the start of the file. 默認值是文件的開頭。 -
nG
: Jump to line number n.nG
:跳轉到第n行。 Default is the end of the file. 默認值是文件的結尾。
So to go to line number 320123, you would type 320123g
. 因此,要轉到第320123行,您需要輸入320123g
。
Copy-pasted straight from Wikipedia . 直接從維基百科複製粘貼。
#6樓
You can use sed
for this too - 你也可以使用sed
-
sed -n '320123'p filename
This will print line number 320123 . 這將打印行號320123 。
If you want a range then you can do - 如果你想要一個範圍那麼你可以做 -
sed -n '320123,320150'p filename
If you want from a particular line to the very end then - 如果你想從特定的線到最後那麼 -
sed -n '320123,$'p filename