使用Unix中的Less来转到特定的行号

本文介绍了如何在大型文件中使用Unix的Less命令迅速跳转到特定行号,如通过'g'命令结合行号,或者利用'+'参数在启动Less时指定行号,提高查找效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自: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 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值