简体   繁体   English

Perl:如何删除输出文件中的最后写入的行?

[英]Perl: how do I remove the last written line in an output file?

I was wondering whether it is possible to remove the last line that was written in the output file with [print OUT "blabla";] in perl? 我想知道是否可以用perl中的[print OUT "blabla";]删除输出文件中写的最后一行? Many thanks in advance! 提前谢谢了!

This will delete the last line from a file: 这将从文件中删除最后一行:

open (FH, "+< $file")               or die "can't update $file: $!";
while ( <FH> ) {
    $addr = tell(FH) unless eof(FH);
}
truncate(FH, $addr)                 or die "can't truncate $file: $!";

另一个建议:推迟打印该行,直到您知道需要打印为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM