Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5376600
  • 博文数量: 1144
  • 博客积分: 11974
  • 博客等级: 上将
  • 技术积分: 12312
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-13 20:06
文章存档

2017年(2)

2016年(14)

2015年(10)

2014年(28)

2013年(23)

2012年(29)

2011年(53)

2010年(86)

2009年(83)

2008年(43)

2007年(153)

2006年(575)

2005年(45)

分类: LINUX

2009-04-05 18:01:06


Description: Oh well, this question keeps popping up every few months. How do I tail a large file with perl? Here is my crack at it, it uses seek from the end of the file. Linux only for now. Bill Gates dosn't seem to care about linux compatibility, so why should I care about windows compatibility?
#!/usr/bin/perl -w
# Simple program to read the last n line(s) of a file. 
# Reads from the end of the file for effeciency 
# "\n" linux only, 
# usage tailz  filename  numberoflines 
use strict;

my $filename = shift or die "Usage: $0 file numlines\n";
my $numlines  = shift;
my $byte;

# Open the file in read mode 
open FILE, "<$filename" or die "Couldn't open $filename: $!";

# Rewind from the end of the file until count of eol 's
seek FILE,-1, 2;  #get past last eol 
my $count=0;
 while (1){
   seek FILE,-1,1;
   read FILE,$byte,1;
   if(ord($byte) == 10 ){$count++;if($count == $numlines){last}}
   seek FILE,-1,1;
 if (tell FILE == 0){last}
}
$/=undef;
my $tail = ;
print "$tail\n";

阅读(1742) | 评论(1) | 转发(0) |
0

上一篇:sock

下一篇:m operator

给主人留下些什么吧!~~

chinaunix网友2009-04-06 09:41:02

一个字节一个字节的读,未免效率太低了 还不如pass两遍,先找出文件总共有多少行,再用个while循环读取最后的几行