perl读取文件夹下的文件

该博客讲述了如何使用Perl读取文件夹file_data下的文件,查找包含特定模式(exciton_dn和exciton_up)的行,对第一列数字求和,第二列数字计算平方差,然后将结果写入out_file.txt。文章强调了正确处理Windows和Linux路径以及正则表达式在匹配文件内容中的作用。
摘要由CSDN通过智能技术生成
#!usr/bin/perl-w
use strict;
my $file_name;#文件名
my $num_1;
my $num_2;
my $num_3;
my $num_4;
my @result=qw//;
my $sum = 0;
my $print_line;#写文件名以及数据处理结果
my $dir_tree = "file_data";#文件夹名
my $file;
my $c_flg1;
my $c_flg2;
my $fi_line;
opendir DR,"$dir_tree" or die "can't open $dir_tree: $!";#打开文件夹
# my @file = readdir DR;
my @file = readdir(DR);#读取文件名到列表中
print "@file";
open (out_file,">","out_file.txt");#打开输出结果文件
foreach $file ( @file ){
    $c_flg1 = 0;
    $c_flg2 = 0;
	print "$file\n";
	 if( $file =~/\.\.?$/ ){#跳过.和..
         print "next";
		 next;
	}
    open (in_file,"<","$dir_tree\\$file")||die "Can't open $dir_tree\\$file: $!";#打开文件,win下用\
    # open (out_file,">","out_file.txt");
    while( <in_file> ){
       if( /^e.*dn:.(-?\d+\.?\d+)\s+(-?\d+\.?\d+)$/g ){#匹配
          $num_1 = $1;
          print "num_1 $num_1\n";
          $num_2 = $2;
	      $c_flg1 =1;
          print "num_2 $num_2\n";
       } 
       if( /^e.*up:.(-?\d+\.?\d+)\s+(-?\d+\.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值