On Tue, Dec 01, 2009 at 09:15:08AM +0800, cncruyff wrote:
>你好!
>
>要是想要取上一行,或者上几行呢?
>我知道gnu的grep有-B参数可以做到,perl如何实现?
>

在Unix-like下, 可以用Perl直接调用grep;
如果完全用perl的话, 可以用原始办法.
打印error的后面行直接打印即可, 若打印前面行
可以先保存下来, 当出错的时候就打印了, 反正都要遍历整个文件.

>2009-12-01 
>
>
>
>cncruyff 
>
>
>
>发件人: Fayland Lam 
>发送时间: 2009-11-30  20:56:48 
>收件人: perlchina 
>抄送: 
>主题: [PerlChina] Re: about open() and print 
> 
>if (/\[error]|error/) {
>print;
>my $line = <LOG>;
>print $line;
>}
>2009/11/30 earthminator <[email protected]>:
>> #!/usr/bin/perl
>> use strict;
>> use warnings;
>> my $log_file = '/export/home/wanghao/1116/LOG.txt';
>> open(LOG,"< $log_file") or die "Unable to open logfile:$!\n";
>> while(<LOG>){
>>    print  if /\[error]|error/;
>> }
>> close(LOG);
>> 写了如上程序,能够成功调出LOG.txt中含有error的行,当是我想把含有error这一行的下一行也同时输出,请问该怎么弄?
>>
>>
>> >
>>
>-- 
>Fayland Lam // http://www.fayland.org/
>
>
>
>
>
>
> 
>
>   
> 江苏省南京市鼓楼区南昌路40号长江科技园3F     Zip.210037          闻 琦
>  3F Changjiang Science Park No.40 Nanchang Road Gulou District Nanjing, China 
>        Wen  Qi
>   Tel.025-83553301   Fax.025-83553310   
>   www.c-platform.com     MP : 13809029763技术支持部        平台维护工程师          
>   E-mail: [email protected] Technical Support Dept.     Platform 
> Maintenance Engineer 
>
>>



--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛“PerlChina Mongers 讨论组”论坛。
 要在此论坛发帖,请发电子邮件到 [email protected]
 要退订此论坛,请发邮件至 [email protected]
 更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---

回复