在 Bash 中打印文件,跳过前 X 行
我有一个很长的文件想要打印,例如跳过前 1,000,000 行。
我查看了 cat 手册页,但没有看到任何选项可以执行此操作。 我正在寻找执行此操作的命令或简单的 Bash 程序。
I have a very long file which I want to print, skipping the first 1,000,000 lines, for example.
I looked into the cat man page, but I did not see any option to do this. I am looking for a command to do this or a simple Bash program.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我发现删除文件前十行的最简单方法:
在一般情况下,X 是要删除的初始行数,这要归功于评论者和编辑:
Easiest way I found to remove the first ten lines of a file:
In the general case where
X
is the number of initial lines to delete, credit to commenters and editors for this:如果您的系统上有可用的 GNU tail,则可以执行以下操作:
+
字符可以执行您想要的操作。 引用手册页:因此,如评论中所述,输入 +1000001 开始打印前 1,000,000 行后的第一项。
If you have GNU tail available on your system, you can do the following:
It's the
+
character that does what you want. To quote from the man page:Thus, as noted in the comment, putting +1000001 starts printing with the first item after the first 1,000,000 lines.
如果您想跳过前两行:
如果您想跳过第 x 行:
If you want to skip first two line:
If you want to skip first x line:
AWK 的简洁版本:
但我建议使用整数。
A less verbose version with AWK:
But I would recommend using integer numbers.
使用 sed
删除
命令带有范围地址。 例如:或者,如果您只想打印已知范围,请使用带有
-n
标志的 print 命令:无论是否存在 GNU 实用程序,此解决方案都应该在所有 Unix 系统上可靠地工作。
Use the sed
delete
command with a range address. For example:Alternatively, if you want to only print a known range, use the print command with the
-n
flag:This solution should work reliably on all Unix systems, regardless of the presence of GNU utilities.
使用:
该命令将删除第一行并打印其余内容。
Use:
This command will delete the first line and print the rest.
如果你想查看前 10 行,你可以使用 sed,如下所示:
或者如果你想查看第 20 到 30 行,你可以使用:
If you want to see the first 10 lines you can use sed as below:
Or if you want to see lines from 20 to 30 you can use:
您可以使用 head 和 tail 命令来执行此操作:
其中 num 是 1e6 + 要打印的行数。
You can do this using the head and tail commands:
where num is 1e6 + the number of lines you want to print.
只是为了提出一个 sed 替代方案。 :) 要跳过前一百万行,请尝试
|sed '1,1000000d'
。例子:
Just to propose a
sed
alternative. :) To skip first one million lines, try|sed '1,1000000d'
.Example:
这个 shell 脚本对我来说效果很好:
与此示例文件 (file.txt) 一起使用:
命令(它将从文件中的第二行到第四行提取):
该命令的输出:
当然,您可以改进它,例如通过测试所有参数值是否符合预期:-)
This shell script works fine for me:
Used with this sample file (file.txt):
The command (it will extract from second to fourth line in the file):
Output of this command:
Of course, you can improve it, for example by testing that all argument values are the expected :-)
我需要做同样的事情并找到了这个线程。
我尝试了“tail -n +”,但它只是打印了所有内容。
更多的+行在提示符上运行得很好,但事实证明,在无头模式(cronjob)下运行时,它的行为完全不同。
我最后自己写了这个:
I needed to do the same and found this thread.
I tried "tail -n +, but it just printed everything.
The more +lines worked nicely on the prompt, but it turned out it behaved totally different when run in headless mode (cronjob).
I finally wrote this myself:
使用
tail
。 一些示例:要跳过前 N 行:
例如,要跳过前 10 行:
要查看最后 N 行,请省略“+”:
Use
tail
. Some examples:To SKIP the first N lines:
For instance, to skip the first 10 lines:
To see the last N lines, omit the "+":