Chinaunix首页 | 论坛 | 博客
  • 博客访问: 517844
  • 博文数量: 126
  • 博客积分: 851
  • 博客等级: 准尉
  • 技术积分: 1287
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-06 11:21
个人简介

个人最新博客地址http://www.skylway.com/

文章分类

全部博文(126)

文章存档

2016年(2)

2014年(60)

2013年(35)

2012年(29)

分类: Python/Ruby

2012-11-26 15:56:15

seek 设置文件的当前位置!
当一个文件非常大时可以从指定位置读起。
seekFILEHANDLE,POSITION,WHENCE  
成功返回真,失败返回假。
POSITION是读入的新位置(字节)。
WHENCE有3个值,0表示新位置是POSITION,1表示当前位置加上POSITION,2表示文件尾加上POSITION
例如:从file.txt的12字节开始读起并打印出来。
open(FILEHANDLE,"seek FILEHANDLE,12,0;
while (){
       print;
}
close (FILEHANDLE);
 
例子:
 

#!/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

 

 

PRINTEDRESULTS:

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


 Tricklocation of keyword
 print this line
 print this line
 print thisline


 Thisis another keyword
 print this line
 print this line
 print this line


 Lastline and a keyword
 
 

 __EndOf File__

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