ITPub博客

首页 > Linux操作系统 > Linux操作系统 > perl文本文件处理

perl文本文件处理

原创 Linux操作系统 作者:shiyikezhan 时间:2011-11-23 18:57:12 0 删除 编辑
最近需要提取出文本文件中某几列的数据,简单写了一下脚本:

use strict;
my $read_file = "1.txt";
my $write_file = "2.txt";
open my ($fhin), '<', $read_file or die "Can't open file: $!";
open my ($fhout),'>', $write_file or die "Can't open file: $!";
while(<$fhin>){ #也可以在foreach循环中使用类似的语法读取整个文件内容到内存,将while改为foreach
chomp;
my @line=split /,/,$_;
my ($col1,$col2)=@line[0,1];
print $fhout "$col1,$col2\n";
}
close $fhout;
close $fhin;

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/24802932/viewspace-711869/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2011-10-16

  • 博文量
    4
  • 访问量
    9392