Linux之Less命令跳转到特定的行号

本文翻译自: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命令的使用。

vi命令Linux/Unix系统中的一款文本编辑器,可用于编辑各种文本文件,包括配置文件、代码等。而less命令则是一种文本查看器,可用于查看文本文件,比vi命令更加轻量级。下面分别对vi和less命令进行详细讲解。 ## vi命令 ### 基本用法 vi命令有多种模式,最常用的是命令模式和编辑模式。在命令模式下,可以执行一些命令,如保存文件、退出编辑等;在编辑模式下,可以进行插入、删除、修改、复制等编辑操作。 以下是vi命令的一些基本用法: 1. 打开文件:`vi 文件名` 2. 切换到编辑模式:按下`i`键或`Insert`键 3. 保存文件:在命令模式下,输入`:w`并按下回车键 4. 退出编辑:在命令模式下,输入`:q`并按下回车键 5. 强制退出编辑(不保存):在命令模式下,输入`:q!`并按下回车键 6. 保存并退出编辑:在命令模式下,输入`:wq`并按下回车键 ### 进阶用法 vi命令还有很多高级用法,如: 1. 复制、粘贴和删除:在编辑模式下,使用`yy`复制当前行,使用`p`粘贴剪切板中的内容,使用`dd`删除当前行 2. 搜索和替换:在命令模式下,输入`/关键字`进行搜索,输入`:s/旧字符串/新字符串/g`进行替换 3. 多窗口编辑:在命令模式下,输入`:sp`可以打开一个新窗口,输入`:vs`可以打开一个新的垂直窗口 ## less命令 less命令是一种文本查看器,用于查看文本文件。它比vi命令更加轻量级,可以快速浏览大型文本文件。 以下是less命令的一些基本用法: 1. 查看文件:`less 文件名` 2. 翻页:使用空格键向下翻页,使用`b`键向上翻页 3. 搜索:按下`/`键后输入要搜索的关键字,按下回车键进行搜索,使用`n`键继续向下查找 4. 退出查看:按下`q`键退出查看 除了基本用法外,less命令还有一些高级用法,如: 1. 显示行号:使用`-N`参数可以在每行前面显示行号,如`less -N 文件名` 2. 显示颜色:使用`-R`参数可以显示颜色,如`less -R 文件名` 3. 显示文件大小:在查看文件时,按下`=`键可以显示文件大小和行数 4. 查看多个文件:在查看多个文件时,可以使用`:n`和`:p`命令来切换文件
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕城南风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值