本文翻译自: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中):

 
  1. g and the line number to go forward

  2. 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 

其他用法:

less 查看着一个文本,或者搜索到了目标,想修改,怎么办,这个时候,只要键盘输入"v"即可,会使用系统默认的编辑工具,马上就可以编辑啦。编辑完保存,会重新退回到less命令的使用。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐