简体   繁体   English

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

[英]Perl - Read multiple files and read line by line of the text file

I am trying to read multiple .txt files in a folder.我正在尝试读取文件夹中的多个 .txt 文件。 Each file should be read line by line, however, I failed to read multiple .txt files by using glob.应该逐行读取每个文件,但是,我无法使用 glob 读取多个 .txt 文件。 Any advice on my code?对我的代码有什么建议吗?

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>;

Currently output:当前输出:

    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.

It failed to get any .txt files.它无法获取任何 .txt 文件。

You are trying to read each file twice: firstly into the hash %data and then again line by line.您正在尝试读取每个文件两次:首先进入散列%data ,然后再逐行读取。

Once you have reached end of file, you have to either reopen the file or use seek to move the read pointer back to the beginning.一旦到达文件末尾,您必须重新打开文件或使用seek将读取指针移回开头。

You also need to set $/ back to its original value, otherwise your loop will read the entire file instead of one line at a time.您还需要将$/设置回其原始值,否则您的循环将读取整个文件而不是一次一行。

It's not clear whether you really need the second copy of the file data in the hash, but you can avoid having to reset $/ by putting the change within a block, like this不清楚您是否真的需要散列中文件数据的第二个副本,但是您可以通过将更改放在一个块中来避免重置$/ ,如下所示

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

and then reset the file pointer to the start again before the while loop.然后在while循环之前再次将文件指针重置为开始。

seek $fh, 0, 0;
#!/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";
    }
}
}

The file globbing works for me.文件通配对我有用。 You might want to specify scope for your @FILES variable and check that there actually are files matching the path you have specified,您可能想要为 @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";
}

your code,你的代码,

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

exit;

This way catches more garbage for about the same amount of code.这种方式可以为大约相同数量的代码捕获更多垃圾。

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) ; 
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM