使用SED获取巨大文本文件的最后n行

时间:2022-06-29 16:50:12

How can I use "sed" command to get the last n lines of a huge text file (e.g. A.txt) and copy them into a new text file(e.g. B.txt)? I do not want to remove that lines from A.txt.

如何使用“sed”命令获取大文本文件的最后n行(例如A.txt)并将它们复制到新的文本文件(例如B.txt)中?我不想从A.txt中删除那些行。

3 个解决方案

#1


25  

You don't. You use tail -n NUMLINES for that.

你没有。你使用tail -n NUMLINES。

tail -n 100 A.txt > B.txt

#2


10  

Here's how to use sed to print the last 10 lines of a file:

以下是使用sed打印文件的最后10行的方法:

sed -e :a -e '$q;N;11,$D;ba' 

You should probably only use this if you're planning on executing more sed commands on these lines. Otherwise the tail command is designed for this job.

如果你计划在这些行上执行更多的sed命令,你应该只使用它。否则tail命令是为此作业设计的。

#3


2  

Using GNU sed, here's how to get the last 10 lines:

使用GNU sed,这里是如何获得最后10行:

(For n lines, replace 11, with n+1)

(对于n行,替换11,用n + 1)

sed -ne':a;$p;N;11,$D;ba' A.txt > B.txt

sed -ne':a; $ p; N; 11,$ D; ba'At.txt> B.txt

Note: On my Mac, with MacPorts, GNU sed is invoked as gsed. To use Apple's sed you would have to separate the label: sed -ne':a' -e'$p;N;11,$D;ba'*

注意:在我的Mac上,使用MacPorts,GNU sed被调用为gsed。要使用Apple的sed,你必须将标签分开:sed -ne':a'-e'$ p; N; 11,$ D; ba'*

Explanation:

sed -ne' invoke sed without automatic printing pattern space

sed -ne'调用没有自动打印模式空间的sed

:a label for looping

:循环标签

$p on last line, print pattern space, then quit

$ p在最后一行,打印模式空间,然后退出

N slurp the next line

N啜饮下一行

11,$D on line 11 through last line, remove the first line from pattern space (i.e. [^\n]*\n)

11,第11行到最后一行的$ D,从模式空间中删除第一行(即[^ \ n] * \ n)

ba' loop to :a

ba'循环到:a

Further

Because of the loop, sed continuously appends the next line to pattern space. After line 11 and through the last line (11,$D) sed begins removing the first line from pattern space. At the last line the pattern space is printed (`$p'), which contains the last 10 most recently slurped lines (the last 10 lines of file A.txt).

由于循环,sed不断地将下一行附加到模式空间。在第11行和最后一行(11,$ D)之后,sed开始从模式空间中删除第一行。在最后一行打印模式空间(`$ p'),其中包含最近10个最近的slurped行(文件A.txt的最后10行)。

#1


25  

You don't. You use tail -n NUMLINES for that.

你没有。你使用tail -n NUMLINES。

tail -n 100 A.txt > B.txt

#2


10  

Here's how to use sed to print the last 10 lines of a file:

以下是使用sed打印文件的最后10行的方法:

sed -e :a -e '$q;N;11,$D;ba' 

You should probably only use this if you're planning on executing more sed commands on these lines. Otherwise the tail command is designed for this job.

如果你计划在这些行上执行更多的sed命令,你应该只使用它。否则tail命令是为此作业设计的。

#3


2  

Using GNU sed, here's how to get the last 10 lines:

使用GNU sed,这里是如何获得最后10行:

(For n lines, replace 11, with n+1)

(对于n行,替换11,用n + 1)

sed -ne':a;$p;N;11,$D;ba' A.txt > B.txt

sed -ne':a; $ p; N; 11,$ D; ba'At.txt> B.txt

Note: On my Mac, with MacPorts, GNU sed is invoked as gsed. To use Apple's sed you would have to separate the label: sed -ne':a' -e'$p;N;11,$D;ba'*

注意:在我的Mac上,使用MacPorts,GNU sed被调用为gsed。要使用Apple的sed,你必须将标签分开:sed -ne':a'-e'$ p; N; 11,$ D; ba'*

Explanation:

sed -ne' invoke sed without automatic printing pattern space

sed -ne'调用没有自动打印模式空间的sed

:a label for looping

:循环标签

$p on last line, print pattern space, then quit

$ p在最后一行,打印模式空间,然后退出

N slurp the next line

N啜饮下一行

11,$D on line 11 through last line, remove the first line from pattern space (i.e. [^\n]*\n)

11,第11行到最后一行的$ D,从模式空间中删除第一行(即[^ \ n] * \ n)

ba' loop to :a

ba'循环到:a

Further

Because of the loop, sed continuously appends the next line to pattern space. After line 11 and through the last line (11,$D) sed begins removing the first line from pattern space. At the last line the pattern space is printed (`$p'), which contains the last 10 most recently slurped lines (the last 10 lines of file A.txt).

由于循环,sed不断地将下一行附加到模式空间。在第11行和最后一行(11,$ D)之后,sed开始从模式空间中删除第一行。在最后一行打印模式空间(`$ p'),其中包含最近10个最近的slurped行(文件A.txt的最后10行)。