如何删除文件的最后5行 [英] How to delete the last 5 lines of a file
本文介绍了如何删除文件的最后5行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有几个命令用perl打印文本到一个文件。在这些 print
命令期间,我有一个if语句,它应该删除当前正在写入的文件的最后5行,如果语句为true。
if($ exists == 0){
print( OUTPUT ???)#这应该删除最后5行
解决方案
您可以使用 Tie :: File :
使用Tie :: File;
绑定我的@array,'Tie :: File',文件名或者$!
if($ exists == 0){
$#array - = 5;
$ b $ p
$ b 打印时可以使用相同的数组,但使用 push
改为:
push @array,text of line;
I have several commands printing text to a file using perl. During these print
commands I have an if statement which should delete the last 5 lines of the file I am currently writing to if the statement is true. The number of lines to delete will always be 5.
if ($exists == 0) {
print(OUTPUT ???) # this should remove the last 5 lines
}
解决方案 You can use Tie::File:
use Tie::File;
tie my @array, 'Tie::File', filename or die $!;
if ($exists == 0) {
$#array -= 5;
}
You can use the same array when printing, but use push
instead:
push @array, "line of text";
这篇关于如何删除文件的最后5行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文