22 1 月 2013

Perl模仿指令grep,找出搜尋字串

作者:Ben哥|發布日期:2009/08/14

Perl模仿指令grep,找出搜尋字串的上五行及下五行?  

#!/usr/bin/perl

open(FILE, "檔案名稱" ) or die "Can't open file\n";

@lines =;

@hit_line = map{$lnum++; /關鍵字/?$lnum-1:() } @lines;

foreach $hit_line ( @hit_line )
{
print "\n\nHit line number is : $hit_line+1\n\n";
for( ( $hit_line – 5 ) .. ($hit_line + 5 ) )
{
print "$lines[$_]";
}
}