在 Unix 中使用 Less 转到特定行号
我有一个大约有百万行的文件。我需要去第320123行查看数据。我该怎么做?
I have a file that has around million lines. I need to go to line number 320123 to check the data. How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
以
n
为行号:ng
:跳转到第 n 行。默认是文件的开头。nG
:跳转到第n行。默认是文件末尾。因此,要转到行号 320123,您需要输入
320123g
。直接从维基百科复制粘贴。
With
n
being the line number:ng
: Jump to line number n. Default is the start of the file.nG
: Jump to line number n. Default is the end of the file.So to go to line number 320123, you would type
320123g
.Copy-pasted straight from Wikipedia.
要直接从命令行打开特定行,请使用:
如果您也想查看行号:
您还可以选择在终端的特定行显示文件的特定行,以便当您需要一些行号时上下文的线条。例如,这将在终端第 10 行打开第 320123 行的文件:
To open at a specific line straight from the command line, use:
If you want to see the line numbers too:
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:
您也可以使用
sed
来实现此目的 -这将打印行号 320123。
如果你想要一个范围,那么你可以这样做 -
如果你想要从特定行到最后,那么 -
You can use
sed
for this too -This will print line number 320123.
If you want a range then you can do -
If you want from a particular line to the very end then -
从 less 内部(在 Linux 中):
单独使用,g 和 G 将分别带您到文件中的第一行和最后一行;与数字一起使用它们都是等效的。
一个例子;你想要转到文件的第 320123 行,
此外,您可以在 less 内输入“-N”来激活/停用行号。事实上,您可以从程序内部传递任何命令行开关,例如 -j 或 -N。
注意:您可以在命令行中提供行号来启动 less (less +number -N),这比在程序内部执行要快得多:
这将打开一个显示行号并从第 12345 行开始的文件
< em>来源:man 1 less 和 less 中的内置帮助(less 418)
From within less (in Linux):
Used alone, g and G will take you to the first and last line in a file respectively; used with a number they are both equivalent.
An example; you want to go to line 320123 of a file,
Additionally you can type '-N' inside less to activate / deactivate the line numbers. You can as a matter of fact pass any command line switches from inside the program, such as -j or -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:
This will open a file displaying the line numbers and starting at line 12345
Source: man 1 less and built-in help in less (less 418)
对于编辑,可以通过命令行中的
+n
在nano
中进行编辑,例如nano +16 file.txt
打开
文件。 txt
到第 16 行。For editing this is possible in
nano
via+n
from command line, e.g.,nano +16 file.txt
To open
file.txt
to line 16.补充一下我的 2 美分,在长文件中,移动到给定百分比会更快,例如输入
40%
移动到长度的40th
百分位。Just to add my 2 cents, in long files it is quicker to move to a given percentage, e.g typing
40%
moves to the40-th
percentile of the length.