在 Bash 中打印文件,跳过前 X 行

发布于 2024-07-14 00:57:57 字数 108 浏览 6 评论 0原文

我有一个很长的文件想要打印,例如跳过前 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

作业与我同在 2024-07-21 00:57:58

我发现删除文件前十行的最简单方法:

$ sed 1,10d file.txt

在一般情况下,X 是要删除的初始行数,这要归功于评论者和编辑:

$ sed 1,Xd file.txt

Easiest way I found to remove the first ten lines of a file:

$ sed 1,10d file.txt

In the general case where X is the number of initial lines to delete, credit to commenters and editors for this:

$ sed 1,Xd file.txt
不及他 2024-07-21 00:57:58

如果您的系统上有可用的 GNU tail,则可以执行以下操作:

tail -n +1000001 huge-file.log

+ 字符可以执行您想要的操作。 引用手册页:

如果 K(字节数或行数)的第一个字符是
`+',从每个文件开头的第 K 个项目开始打印。

因此,如评论中所述,输入 +1000001 开始打印前 1,000,000 行后的第一项。

If you have GNU tail available on your system, you can do the following:

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.

Thus, as noted in the comment, putting +1000001 starts printing with the first item after the first 1,000,000 lines.

感性 2024-07-21 00:57:58

如果您想跳过前两行:

tail -n +3 <filename>

如果您想跳过第 x 行:

tail -n +$((x+1)) <filename>

If you want to skip first two line:

tail -n +3 <filename>

If you want to skip first x line:

tail -n +$((x+1)) <filename>
长梦不多时 2024-07-21 00:57:58

AWK 的简洁版本:

awk 'NR > 1e6' myfile.txt

但我建议使用整数。

A less verbose version with AWK:

awk 'NR > 1e6' myfile.txt

But I would recommend using integer numbers.

帝王念 2024-07-21 00:57:58

使用 sed 删除命令带有范围地址。 例如:

sed 1,100d file.txt # Print file.txt omitting lines 1-100.

或者,如果您只想打印已知范围,请使用带有 -n 标志的 print 命令:

sed -n 201,300p file.txt # Print lines 201-300 from file.txt

无论是否存在 GNU 实用程序,此解决方案都应该在所有 Unix 系统上可靠地工作。

Use the sed delete command with a range address. For example:

sed 1,100d file.txt # Print file.txt omitting lines 1-100.

Alternatively, if you want to only print a known range, use the print command with the -n flag:

sed -n 201,300p file.txt # Print lines 201-300 from file.txt

This solution should work reliably on all Unix systems, regardless of the presence of GNU utilities.

毅然前行 2024-07-21 00:57:58

使用:

sed -n '1d;p'

该命令将删除第一行并打印其余内容。

Use:

sed -n '1d;p'

This command will delete the first line and print the rest.

2024-07-21 00:57:58

如果你想查看前 10 行,你可以使用 sed,如下所示:

sed -n '1,10 p' myFile.txt

或者如果你想查看第 20 到 30 行,你可以使用:

sed -n '20,30 p' myFile.txt

If you want to see the first 10 lines you can use sed as below:

sed -n '1,10 p' myFile.txt

Or if you want to see lines from 20 to 30 you can use:

sed -n '20,30 p' myFile.txt
为你拒绝所有暧昧 2024-07-21 00:57:58

您可以使用 head 和 tail 命令来执行此操作:

head -n <num> | tail -n <lines to print>

其中 num 是 1e6 + 要打印的行数。

You can do this using the head and tail commands:

head -n <num> | tail -n <lines to print>

where num is 1e6 + the number of lines you want to print.

情独悲 2024-07-21 00:57:58

只是为了提出一个 sed 替代方案。 :) 要跳过前一百万行,请尝试 |sed '1,1000000d'

例子:

$ perl -wle 'print for (1..1_000_005)'|sed '1,1000000d'
1000001
1000002
1000003
1000004
1000005

Just to propose a sed alternative. :) To skip first one million lines, try |sed '1,1000000d'.

Example:

$ perl -wle 'print for (1..1_000_005)'|sed '1,1000000d'
1000001
1000002
1000003
1000004
1000005
紫﹏色ふ单纯 2024-07-21 00:57:58

这个 shell 脚本对我来说效果很好:

#!/bin/bash
awk -v initial_line=$1 -v end_line=$2 '{
    if (NR >= initial_line && NR <= end_line) 
    print $0
}' $3

与此示例文件 (file.txt) 一起使用:

one
two
three
four
five
six

命令(它将从文件中的第二行到第四行提取):

edu@debian5:~$./script.sh 2 4 file.txt

该命令的输出:

two
three
four

当然,您可以改进它,例如通过测试所有参数值是否符合预期:-)

This shell script works fine for me:

#!/bin/bash
awk -v initial_line=$1 -v end_line=$2 '{
    if (NR >= initial_line && NR <= end_line) 
    print $0
}' $3

Used with this sample file (file.txt):

one
two
three
four
five
six

The command (it will extract from second to fourth line in the file):

edu@debian5:~$./script.sh 2 4 file.txt

Output of this command:

two
three
four

Of course, you can improve it, for example by testing that all argument values are the expected :-)

痴者 2024-07-21 00:57:58
cat < File > | awk '{if(NR > 6) print $0}'
cat < File > | awk '{if(NR > 6) print $0}'
半边脸i 2024-07-21 00:57:58

我需要做同样的事情并找到了这个线程。

我尝试了“tail -n +”,但它只是打印了所有内容。

更多的+行在提示符上运行得很好,但事实证明,在无头模式(cronjob)下运行时,它的行为完全不同。

我最后自己写了这个:

skip=5
FILE="/tmp/filetoprint"
tail -n$((`cat "${FILE}" | wc -l` - skip)) "${FILE}"

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:

skip=5
FILE="/tmp/filetoprint"
tail -n$((`cat "${FILE}" | wc -l` - skip)) "${FILE}"
一向肩并 2024-07-21 00:57:57

使用 tail。 一些示例:

$ tail file.log
< Last 10 lines of file.log >

要跳过前 N 行:

$ tail -n +<N+1> <filename>
< filename, excluding first N lines. >

例如,要跳过前 10 行:

$ tail -n +11 file.log
< file.log, starting at line 11, after skipping the first 10 lines. >

要查看最后 N 行,请省略“+”:

$ tail -n <N> <filename>
< last N lines of file. >

Use tail. Some examples:

$ tail file.log
< Last 10 lines of file.log >

To SKIP the first N lines:

$ tail -n +<N+1> <filename>
< filename, excluding first N lines. >

For instance, to skip the first 10 lines:

$ tail -n +11 file.log
< file.log, starting at line 11, after skipping the first 10 lines. >

To see the last N lines, omit the "+":

$ tail -n <N> <filename>
< last N lines of file. >
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文