Perl - 读取多个文件并逐行读取文本文件

时间:2014-04-09 01:21:42

标签: perl

我正在尝试读取文件夹中的多个.txt文件。每个文件都应该逐行读取,但是,我无法通过使用glob读取多个.txt文件。关于我的代码的任何建议?

my %data;
@FILES = glob("*.txt");

$EmailMsg .= "EG. Folder(week) = Folder(CW01) --CW01 = Week 1 -- Number is week\n ";
$EmailMsg .= "=======================================================================================================\n";

# Try to Loop multiple files here
foreach my $file (@FILES) {
  local $/ = undef;
  open my $fh, '<', $file;
  $data{$file} = <$fh>;

  # Read the file one line at a time.
  while (my $line = <$fh>) {
    chomp $line;
    $line =~ s/^\s+//;
    $line =~ s/\s+$//;
    my ($name, $date, $week) = split /\:/, $line;

    if ($name eq "NoneFolder") {
      $EmailMsg .= "Folder ($week) - No Folder created on the FTP! Failed to open folder!\n";
    }

    if ($name eq "EmptyFiles") {
      $EmailMsg .= "Folder ($week) - No Files insides the folder! Failed download files!\n";
    }

  }
}
$EmailMsg .= "=======================================================================================================\n";
$EmailMsg .= "Please note that if you receive this email means that the script is running fine just that no folder is created or no files inside the folder for the week on the FTP.\n";

# close the file.
#close <$fh>;

目前输出:

    EG. Folder(week) = Folder(CW01) --CW01 = Week 1 -- Number is week
  =======================================================================================================
    =======================================================================================================
    Please note that if you receive this email means that the script is running fine just that no folder is created or no files inside the folder for the week on the FTP.

无法获取任何.txt文件。

4 个答案:

答案 0 :(得分:3)

您尝试两次读取每个文件:首先进入哈希%data然后再逐行阅读。

到达文件末尾后,您必须重新打开文件或使用seek将读取指针移回开头。

您还需要将$/设置回其原始值,否则您的循环将一次读取整行文件而不是一行。

目前尚不清楚您是否确实需要哈希中文件数据的第二个副本,但您可以通过将更改置于块中来避免重置$/,如此

open my $fh, '<', $file;
$data{$file} = do {
  local $/ = undef;
  <$fh>;
};

然后在while循环之前将文件指针重新设置为start。

seek $fh, 0, 0;

答案 1 :(得分:1)

#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
my @files=('Read a file.pl','Read a single text file.pl','Read only one 
file.pl','Read the file using while.pl','Reading the file.pl');
foreach my $i(@files) {
open(FH, "<$i");
{
    while (my $row = <FH>) {
        chomp $row;
        print "$row\n";
    }
}
}

答案 2 :(得分:0)

文件globbing适用于我。您可能希望为@FILES变量指定范围,并检查实际上是否存在与您指定的路径匹配的文件,

#!/bin/env perl
use strict;
use warnings;

## glob on all files in home directory
## see: http://perldoc.perl.org/File/Glob.html
use File::Glob ':globally';
my @configs = <~myname/project/etc/*.cfg>;

foreach my $fn (@configs) {
    print "file $fn\n";
}

你的代码,

my %data;
#here are some .c files,
my @FILES = glob("../*.c");
foreach my $fn (@FILES) {
    print "file $fn\n";
}

exit;

答案 3 :(得分:0)

这种方式可以捕获更多垃圾以获得大约相同数量的代码。

my $PATH = shift @ARGV ; 
chomp $PATH ; 

opendir(TXTFILE,$PATH) || die ("failed to opendir: $PATH") ; 
my @file = readdir TXTFILE ; 
closedir(TXTFILE) ; 

foreach(@file) { # 
   next unless ($_ =~ /\.txt$/i) ; # Only get .txt files
   $PATH =~ s/\/$//g ; $PATH =~ s/$/\// ; # Uniform trailing slash
   my $thisfile = $PATH . $_ ; # now a fully qualified filename 

   unless (open(THISFILE,$thisfile)) { # Notify on busted files.
      warn ("$thisfile failed to open") ;
      next ; 
   }

   while(<THISFILE>) { 
       # etc. etc. 
   }

   close(THISFILE) ; 
 }