Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5273596
  • 博文数量: 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-11-29 16:45:55

perl中seek函数的用法(2009-09-10 16:22:47)
seek 设置文件的当前位置!
当一个文件非常大时可以从指定位置读起。
seek FILEHANDLE,POSITION,WHENCE   
成功返回真,失败返回假。
POSITION 是读入的新位置(字节)。
WHENCE有3个值,0表示新位置是POSITION,1表示当前位置加上POSITION,2表示文件尾加上POSITION
例如:从file.txt的12字节开始读起并打印出来。
open (FILEHANDLE,"seek FILEHANDLE,12,0;
while (){
        print;
}
close (FILEHANDLE);
 
例子:
 
Read Line And Selected Subsequent Lines
#!/usr/bin/perl

open (TEST, "while () {
   if (index ($_, "keyword") > -1) {
    $position = tell(TEST);
    $keyword_line = $_;
    $line_1 = ;
    $line_2 = ;
    $line_3 = ;
    print " $keyword_line $line_1 $line_2 $line_3\n\n";
    if (!($line_1)) { print " __End Of File__"; last; }
    if (!($line_2)) { print " __End Of File__"; last; }
    if (!($line_3)) { print " __End Of File__"; last; }
    seek(TEST, $position, 0);
   }
}
close (TEST);
exit;


test.txt:

skip this line
A keyword is here
print this line
Trick location of keyword
print this line
print this line
print this line
skip this line
skip this line
This is another keyword
print this line
print this line
print this line
skip this line
Last line and a keyword

 

 

PRINTED RESULTS:

A keyword is here
 print this line
 Trick location of keyword
 print this line


 Trick location of keyword
 print this line
 print this line
 print this line


 This is another keyword
 print this line
 print this line
 print this line


 Last line and a keyword
 
 

阅读(1055) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~