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

时间:2020-12-09 15:40:38

I have a very long file which I want to print but skipping the first 1e6 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.

我有一个很长的文件,我想打印它,但是跳过了第一行1e6行。我查看了猫的页面,但我没有看到任何选择。我正在寻找执行此操作的命令或简单的bash程序。

12 个解决方案

#1


575  

You'll need tail.

你需要的尾巴。

$ tail great-big-file.log
< Last 10 lines of great-big-file.log >

If you really need to SKIP a particular number of 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. >

#2


79  

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.

因此,正如注释中所指出的,put +1000001开始在第一个1,000,000行之后打印第一个条目。

#3


62  

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

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

$ sed 1,10d file.txt

#4


19  

A less verbose version with AWK:

一个更详细的AWK版本:

awk 'NR > 1e6' myfile.txt

But I would recommend using integer numbers.

但是我建议使用整数。

#5


13  

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

只是提出一个sed的替代方案。:)要跳过前一百万行,试试|“1000000d”。

Example:

例子:

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

#6


12  

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

如果你想看到前10行,可以使用以下的sed:

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

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

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

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

#7


12  

if you want to skip first two line
tail -n +3 <filename>

如果要跳过前两行-n +3 <文件名>

if you want to skip first x line
tail -n +$((x+1)) <filename>

如果您想跳过第x行尾部-n +$((x+1)) <文件名>

#8


10  

This shell script works fine for me:

这个shell脚本很适合我:

#!/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):

用于此示例文件(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 :-)

当然,您可以改进它,例如通过测试所有参数值都是期望的:-)

#9


8  

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

使用带范围地址的sed delete命令。例如:

$ 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:

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

$ 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.

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

#10


5  

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.

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

#11


3  

cat < File > | awk '{if(NR > 6) print $0}'

#12


-2  

I needed to do the same and found this thread.

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

I tried "tail -n +, but it just printed everything.

我试过“尾巴-n +”,但它只打印了所有东西。

The more +lines worked nicely on the prompt, but it turned out it behaved totally different when run in headless mode (cronjob).

更多的+行在提示符上运行得很好,但是在无头模式(cronjob)下运行时,结果却完全不同。

I finally wrote this myself:

我终于自己写下了这句话:

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

#1


575  

You'll need tail.

你需要的尾巴。

$ tail great-big-file.log
< Last 10 lines of great-big-file.log >

If you really need to SKIP a particular number of 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. >

#2


79  

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.

因此,正如注释中所指出的,put +1000001开始在第一个1,000,000行之后打印第一个条目。

#3


62  

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

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

$ sed 1,10d file.txt

#4


19  

A less verbose version with AWK:

一个更详细的AWK版本:

awk 'NR > 1e6' myfile.txt

But I would recommend using integer numbers.

但是我建议使用整数。

#5


13  

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

只是提出一个sed的替代方案。:)要跳过前一百万行,试试|“1000000d”。

Example:

例子:

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

#6


12  

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

如果你想看到前10行,可以使用以下的sed:

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

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

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

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

#7


12  

if you want to skip first two line
tail -n +3 <filename>

如果要跳过前两行-n +3 <文件名>

if you want to skip first x line
tail -n +$((x+1)) <filename>

如果您想跳过第x行尾部-n +$((x+1)) <文件名>

#8


10  

This shell script works fine for me:

这个shell脚本很适合我:

#!/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):

用于此示例文件(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 :-)

当然,您可以改进它,例如通过测试所有参数值都是期望的:-)

#9


8  

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

使用带范围地址的sed delete命令。例如:

$ 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:

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

$ 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.

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

#10


5  

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.

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

#11


3  

cat < File > | awk '{if(NR > 6) print $0}'

#12


-2  

I needed to do the same and found this thread.

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

I tried "tail -n +, but it just printed everything.

我试过“尾巴-n +”,但它只打印了所有东西。

The more +lines worked nicely on the prompt, but it turned out it behaved totally different when run in headless mode (cronjob).

更多的+行在提示符上运行得很好,但是在无头模式(cronjob)下运行时,结果却完全不同。

I finally wrote this myself:

我终于自己写下了这句话:

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