使用计数器获取文件夹中多个文件的文件的第二行

时间:2016-03-22 09:46:15

标签: perl

我正试图从文件夹中获取每个文件的所有第二行。 文件夹中的文件很少,我希望获得第二行中的所有数据并生成单个文件。 但我的问题是当我运行脚本时,我只从文件中获取一个数据。 我无法从所有文件中获取所有数据。需要帮助。

#!/usr/bin/perl

use strict;
use warnings;

my $first_line = 1;
my $last_line = 1;
my $file1; my $filename; my @file;
my $line; #read line
my $no_files=0; # total number of files
my $input_file; #input , output
my $directory = 'C:\Users\AAA';
my $count = 0;

opendir (DIR, $directory) or die $!;
while (my $file = readdir(DIR))
{
     if ($file =~ m/(sum\_)(\S+)\_(\S+)\_(\S+)\_\_(\d+)\_(\d+)\.txt$/)
    {
        $filename = "$directory\\$`$&"; # take text filename
            print "reading******** $filename\n";
            push (@file, "$filename") # save into array
    }
}


foreach $file1(@file)
{
    $input_file = $file1;
    open(INPUT, "$input_file") or die "$!";     # parse in txt file
    $no_files =$no_files + 1;
    print "foreach* $input_file\n";
    seek $input_file, 0, 0;
while ( <INPUT>)
{
  print "Beginning****** $input_file\n";
  print "$count\n";
  if ($count > $last_line)     # exit the program when you get to the last line
  {
    $count=0;
    close INPUT;
    exit;
  }

if ($count >= $first_line)  # print the current line if the line number is greater than our first param
{
print $_;
}
   $count++;  # increment the line counter
}

close INPUT;

} #foreach

输入文件示例 文件1

  Summary report
  12/05/2015 09:11:28
       Prog Name:    AAAF.xls
       Job Name:     FT_ROOM
       Lot:          U5AAAY_1A

输入文件示例 文件2

  Summary report
  12/05/2016 09:11:28
       Prog Name:    AAAF.xls
       Job Name:     FT_ROOM
       Lot:          U5AAAY_1A

输入文件示例 文件3

  Summary report
  12/05/2017 09:11:28
       Prog Name:    AAAF.xls
       Job Name:     FT_ROOM
       Lot:          U5AAAY_1A

输出

reading******** C:\Users\csleow\Desktop\P9038\FT_ROOM_sum_U59925MY_1A_K4B7501__12052015_042219.txt
reading******** C:\Users\csleow\Desktop\P9038\FT_ROOM_sum_U61853PY_2B_K4B7521__03112016_173308.txt
foreach* C:\Users\csleow\Desktop\P9038\FT_ROOM_sum_U59925MY_1A_K4B7501__12052015_042219.txt
Beginning****** C:\Users\csleow\Desktop\P9038\FT_ROOM_sum_U59925MY_1A_K4B7501__12052015_042219.txt
0
Beginning****** C:\Users\csleow\Desktop\P9038\FT_ROOM_sum_U59925MY_1A_K4B7501__12052015_042219.txt
1
12/05/2015 09:11:28
Beginning****** C:\Users\csleow\Desktop\P9038\FT_ROOM_sum_U59925MY_1A_K4B7501__12052015_042219.txt
2

输出我想看到:

12/05/2015 09:11:28
12/05/2016 09:11:28
12/05/2017 09:11:28

2 个答案:

答案 0 :(得分:1)

只需使用glob

即可
my $foldername = "folder";

foreach my $m (glob("$foldername/*"))
{
    open my $fh, "<", $m or die "$_ $!";
    <$fh>;
    my $second = <$fh>;
    ($fname) = $m=~m/\/(.+)/g;  
    print "$fname $second";
}

glob返回目录中的文件列表。

所以foreach迭代文件夹中的每个文件。

打开文件后,我不断使用文件句柄。原因是你想要一个文件中的第二行数据,所以<$fh>将返回文件的第一行。然后内容的第二行存储到$second。然后它会打印

答案 1 :(得分:-1)

来自mkHun的稍微调整的例子:

my $foldername = shift @ARGV;

foreach my $filename (glob("$foldername/*"))
{
    if(  open my $handler, "<", $filename )
    {
        <$handler>;
        print scalar <$handler>;
        close $handler;
    }
    else
    {
        warn "Error opening $filename: $!";
    }
}

您正在将foldername作为参数传递给脚本并将输出传递给stdout。