Linux 在 Bash 中打印文件,跳过前 X 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/604864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print a file, skipping the first X lines, in Bash
提问by Eduardo
I have a very long file which I want to print, but skipping the first 1,000,000 lines, for example. I look 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.
例如,我有一个很长的文件要打印,但跳过前 1,000,000 行。我查看了 cat 手册页,但没有看到任何选项可以执行此操作。我正在寻找执行此操作的命令或简单的 Bash 程序。
采纳答案by SingleNegationElimination
You'll need tail. Some examples:
你需要尾巴。一些例子:
$ tail great-big-file.log
< Last 10 lines of great-big-file.log >
If you really need to SKIP a particular number of "first" lines, use
如果您确实需要跳过特定数量的“第一”行,请使用
$ tail -n +<N+1> <filename>
< filename, excluding first N lines. >
That is, if you want to skip N lines, you start printing line N+1. Example:
也就是说,如果要跳过 N 行,则开始打印第 N+1 行。例子:
$ tail -n +11 /tmp/myfile
< /tmp/myfile, starting at line 11, or skipping the first 10 lines. >
If you want to just see the last so many lines, omit the "+":
如果您只想查看最后这么多行,请省略“+”:
$ tail -n <N> <filename>
< last N lines of file. >
回答by Dana the Sane
You can do this using the head and tail commands:
您可以使用 head 和 tail 命令执行此操作:
head -n <num> | tail -n <lines to print>
where num is 1e6 + the number of lines you want to print.
其中 num 是 1e6 + 要打印的行数。
回答by Eddie
If you have GNU tail available on your system, you can do the following:
如果您的系统上有 GNU tail 可用,您可以执行以下操作:
tail -n +1000001 huge-file.log
It's the +
character that does what you want. To quote from the man page:
这+
是做你想做的事的角色。引用手册页:
If the first character of K (the number of bytes or lines) is a `+', print beginning with the Kth item from the start of each file.
如果 K 的第一个字符(字节数或行数)是“+”,则从每个文件开头的第 K 个项目开始打印。
Thus, as noted in the comment, putting +1000001 starts printing with the first item after the first 1,000,000 lines.
因此,如评论中所述,放置 +1000001 开始打印前 1,000,000 行之后的第一项。
回答by sourcerebels
This shell script works fine for me:
这个 shell 脚本对我来说很好用:
#!/bin/bash
awk -v initial_line= -v end_line= '{
if (NR >= initial_line && NR <= end_line)
print one
two
three
four
five
six
}'
Used with this sample file (file.txt):
与此示例文件 (file.txt) 一起使用:
edu@debian5:~$./script.sh 2 4 file.txt
The command (it will extract from second to fourth line in the file):
命令(它将从文件的第二行到第四行提取):
two
three
four
Output of this command:
此命令的输出:
skip=5
FILE="/tmp/filetoprint"
tail -n$((`cat "${FILE}" | wc -l` - skip)) "${FILE}"
Of course, you can improve it, for example by testing that all argument values are the expected :-)
当然,您可以改进它,例如通过测试所有参数值是否符合预期:-)
回答by sourcerebels
I needed to do the same and found this thread.
我需要做同样的事情并找到了这个线程。
I tried "tail -n +, but it just printed everything.
我试过“tail -n +”,但它只是打印了所有内容。
The more +lines worked nicely on the prompt, but it turned out it behaved totally different when run in headless mode (cronjob).
更多的 +lines 在提示上工作得很好,但结果证明在无头模式(cronjob)下运行时它的行为完全不同。
I finally wrote this myself:
我终于自己写了这个:
$ perl -wle 'print for (1..1_000_005)'|sed '1,1000000d'
1000001
1000002
1000003
1000004
1000005
回答by tuomassalo
Just to propose a sed
alternative. :) To skip first one million lines, try |sed '1,1000000d'
.
只是提出一个sed
替代方案。:) 要跳过前一百万行,请尝试|sed '1,1000000d'
.
Example:
例子:
$ sed 1,10d file.txt
回答by David Parks
Easiest way I found to remove the first ten lines of a file:
我发现删除文件前十行的最简单方法:
cat < File > | awk '{if(NR > 6) print sed -n '1,10 p' myFile.txt
}'
回答by aamadeo
sed -n '20,30 p' myFile.txt
回答by Kadir YILDIZ
If you want to see the first 10 lines you can use sed as below:
如果您想查看前 10 行,您可以使用 sed 如下:
awk 'NR > 1e6' myfile.txt
Or if you want to see lines from 20 to 30 you can use:
或者,如果您想查看 20 到 30 行,您可以使用:
##代码##回答by newtover
A less verbose version with AWK:
使用 AWK 的不那么冗长的版本:
##代码##But I would recommend using integer numbers.
但我建议使用整数。