如何删除文件的最后N行? [英] How can I delete the last N lines of a file?

查看:159
本文介绍了如何删除文件的最后N行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以提供一些有关如何从Perl中的文件中删除最后n行的提示吗?我有一个很大的文件,大约400 MB,我想从中删除最后的125,000行.

Can someone give some hints of how to delete the last n lines from a file in Perl? I have a very large file of around 400 MB, and I want to delete some 125,000 last lines from it.

推荐答案

您可以使用领带: :File 将文件作为数组处理.

You can use Tie::File to handle the file as an array.

use Tie::File;
tie (@File, 'Tie::File', $Filename);
splice (@File, -125000, 125000);
untie @File;

一种替代方法是在外壳中使用headwc -l.

An alternative is to use head and wc -l in the shell.

grepsedawk提醒我们head-n选项,无需wc:

edit: grepsedawk reminds us of the -n option to head, no wc necessary:

head -n -125000 FILE > NEWFILE

这篇关于如何删除文件的最后N行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆