【发布时间】:2010-12-08 17:43:32
【问题描述】:
我想从一个大文本文件中删除一大块行。我知道开始和结束行号。将内容(A 和 B 之间的行)导出到某个文件的最优雅的方法是什么?
我知道 head 和 tail 命令 - 还有更快(一步)的方法吗?
文件超过 5GB,包含超过 81 条 mio 行。
已更新:结果
time sed -n 79224100,79898190p BIGFILE.log > out4.log
real 1m9.988s
time tail -n +79224100 BIGFILE.log | head -n +`expr 79898190 - 79224100` > out1.log
real 1m11.623s
time perl fileslice.pl BIGFILE.log 79224100 79898190 > out2.log
real 1m13.302s
time python fileslice.py 79224100 79898190 < BIGFILE.log > out3.log
real 1m13.277s
获胜者是 sed。最快的,最短的。我认为 Chuck Norris 会使用它。
【问题讨论】:
-
有人在 3..2..1.. 中提出了 PERL 单行代码。
-
很高兴看到它们都在几秒钟之内。
-
我有同样的问题:第二个 sed 解决方案更好:它不解析所有文件: time sed -n "16265315,16271356 p" diff.txt real 0m9.180s time sed -n " 16265315,$ p; 16271356 q" diff.txt 真实 0m2.064s