免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3733 | 回复: 10
打印 上一主题 下一主题

File::Tail的read和$/的疑问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-09-08 14:58 |只看该作者 |倒序浏览
现在用File::Tail,想输出包含某个关键字的输出,并用$/控制行输入符。
但自己测试,$/并没有生效。个人估计是File::Tail中没有重新$/。
请问各位怎么解决?


测试代码如下:

#!/usr/bin/perl -w

use strict;
use File::Tail;

die "$0 must have 2 args:RE FILE\n" unless @ARGV==2;
local $/=")\n";
local $|=1;
my $re=shift;
my $file=shift;
die "$file is not exist,please check!\n" unless -e $file;
my $fh=File::Tail->new("$file");
while (defined(my $line=$fh->tail)) {
        print "$line" if $line=~/$re/i;
}



因为我的输入文件格式特殊,所以用local $/=")\n";

论坛徽章:
78
双子座
日期:2013-10-15 08:50:09天秤座
日期:2013-10-16 18:02:08白羊座
日期:2013-10-18 13:35:33天蝎座
日期:2013-10-18 13:37:06狮子座
日期:2013-10-18 13:40:31双子座
日期:2013-10-22 13:58:42戌狗
日期:2013-10-22 18:50:04CU十二周年纪念徽章
日期:2013-10-24 15:41:34巨蟹座
日期:2013-10-24 17:14:56处女座
日期:2013-10-24 17:15:30双子座
日期:2013-10-25 13:49:39午马
日期:2013-10-28 15:02:15
2 [报告]
发表于 2010-09-08 16:19 |只看该作者
TO BE DONE
Planned for 1.0: Using $/ instead of \n to separate "lines" (which should make it possible to read wtmp type files). Except that I discovered I have no need for that enhancement If you do, feel free to send me the patches and I'll apply them - if I feel they don't add too much processing time.


cpan上最后的一句话

论坛徽章:
0
3 [报告]
发表于 2010-09-08 16:23 |只看该作者
我确认不是$/的问题。

如下代码的$/是生效的。

#!/usr/bin/perl -w

use strict;

die "$0 must have 2 args:RE FILE\n" unless @ARGV==2;
local $/=")\n";
my $re=shift;
my $file=shift;
die "$file is not exist,please check!\n" unless -e $file;
open my $fh,"< $file" or die "$!";
while (<$fh>) {
        print "$_" if /$re/i;
}
close $fh;

论坛徽章:
78
双子座
日期:2013-10-15 08:50:09天秤座
日期:2013-10-16 18:02:08白羊座
日期:2013-10-18 13:35:33天蝎座
日期:2013-10-18 13:37:06狮子座
日期:2013-10-18 13:40:31双子座
日期:2013-10-22 13:58:42戌狗
日期:2013-10-22 18:50:04CU十二周年纪念徽章
日期:2013-10-24 15:41:34巨蟹座
日期:2013-10-24 17:14:56处女座
日期:2013-10-24 17:15:30双子座
日期:2013-10-25 13:49:39午马
日期:2013-10-28 15:02:15
4 [报告]
发表于 2010-09-08 16:27 |只看该作者
File::Tail这个包不支持$/

论坛徽章:
0
5 [报告]
发表于 2010-09-08 16:28 |只看该作者
能不能修改这个包里的$/?
该怎么解决这个问题呢?

论坛徽章:
0
6 [报告]
发表于 2010-09-09 09:00 |只看该作者
自己顶下

论坛徽章:
0
7 [报告]
发表于 2010-09-09 09:53 |只看该作者
本帖最后由 黑色阳光_cu 于 2010-09-09 09:59 编辑

可以看到File::Tail里的read方法,调用了readin方法,而readin方法又是用sysread读文件的,sysread不会理$/,read和readin自己处理行尾,硬编码行尾为\n,所以。

论坛徽章:
0
8 [报告]
发表于 2010-09-09 10:31 |只看该作者
谢谢!

请问怎么实现我的需求呢? 我现在还没有能力自己编写package .

谢谢!

论坛徽章:
0
9 [报告]
发表于 2010-09-09 10:49 |只看该作者
本帖最后由 黑色阳光_cu 于 2010-09-09 10:50 编辑
谢谢!

请问怎么实现我的需求呢? 我现在还没有能力自己编写package .

谢谢!
gaochong 发表于 2010-09-09 10:31

  1. #!/usr/bin/perl

  2. use strict;
  3. use warnings;
  4. use File::Tail;

  5. die "$0 must have 2 args:RE FILE\n" unless (@ARGV==2);
  6. my ($re, $file) = @ARGV;
  7. die "$file is not exist,please check!\n" unless -e $file;

  8. local $/=")\n";
  9. local $|=1;

  10. my $fh=File::Tail->new("$file");
  11. my $chunk = "";
  12. while (defined (my $line = $fh->tail()))
  13. {
  14.         $chunk .= $line;
  15.         if (index($chunk, $/) != -1)
  16.         {
  17.                 print $chunk if ($chunk =~ /$re/i);
  18.                 $chunk = "";
  19.         }
  20. }

  21. if ($chunk ne "")
  22. {
  23.         print $chunk if ($chunk =~ /$re/i);
  24.         $chunk = "";
  25. }

复制代码

论坛徽章:
0
10 [报告]
发表于 2010-09-09 11:57 |只看该作者
本帖最后由 gaochong 于 2010-09-09 12:03 编辑

回复 9# 黑色阳光_cu


   
谢谢!
但我试过了,不行。


我的文件格式是这样的,是radius的日志。如果是Login OK的,就只有一行,包含abc1,并以)结尾;如果是Login incorrect的,会有2行信息,如下,第一行包含错误信息,以]结果,第二行包含abcd1,并以)结尾。
Thu Sep  9 11:47:39 2010 : Auth: Login OK: [xxxx/xxxx>] (from client abc1 port 3271558046 cli)
Thu Sep  9 11:47:39 2010 : Error: Username [dsffd] Reject Message[016:User subscription StopState error]
Thu Sep  9 11:47:39 2010 : Auth: Login incorrect: [dsffd/dddd>] (from client abcd1 port 4076864317 cli)
。。。。。。。。。。。。。。。。。。。。。。。。


我的需求如下:
我希望能够tail这个文件,输出包含abcd1的行,并输出这行信息的上一行(错误信息)。



my ($re,$file)=@ARGV;
my $fh=File::Tail->new("$file");
while (defined(my $line=$fh->read)) {
        if ($line=~/$re/i)
        {
                ###怎么输出当前行的上一行?
                print $line;
        }
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP