简体   繁体   English

使用 perl 从文件中读取一行

[英]Reading a line from a file using perl

First off, I have to find the existence of the pass and fail files in the subdirectories.首先,我必须找到子目录中是否存在通过和失败文件。 Then, I need to read the first line of the pass/fail file.然后,我需要阅读通过/失败文件的第一行。 I thought of separating the $file1 and $file to differentiate it.我想将$file1$file分开来区分它。 I'm very new to perl so I know my approach is very bad.我对 perl 很陌生,所以我知道我的方法很糟糕。

I trying to figure out how to combine my current code to read the files I checked exists.我试图弄清楚如何结合我当前的代码来读取我检查过的文件。

use strict;
use File::Find 'find';

my $file = 'pass.txt';
my $file1 = 'fail.txt';
my @directory = ('ram1','ram2');

sub check 
{
    if ( -e $_ && $_ eq $file ) 
        {                                                                      
            print "Found file '$_' in directory '$File::Find::dir'\n";                                     
        }  
    elsif ( -e $_ && $_ eq $file1 ) 
        {                                                                      
            print "Found file '$_' in directory '$File::Find::dir'\n";                                     
        }  
}
find (\&check,@directory);

Is it possible I use the code below for the first if condition?我可以将下面的代码用于第一个 if 条件吗? I know it doesn't work but I'm not sure what to do next as the fail and pass text are inside the directories.我知道它不起作用,但我不确定接下来要做什么,因为失败和通过文本在目录中。

if (open my $File::Find::dir, '<', $file){ 
my $firstLine = <$File::Find::dir>; 
close $firstLine;

Any suggestions would be helpful!任何的意见都将会有帮助!

If you just want to look just in ram1 and ram2 , there's no point in using File::Find.如果您只想查看ram1ram2 ,则使用 File::Find 毫无意义。 That is used for recursive searches, meaning if you want to search all the subdirectories of ram1 and ram2 .这用于递归搜索,这意味着如果您想搜索ram1ram2所有子目录。 (And for that, I'd use File::Find::Rule over File::Find; it's much cleaner.) (为此,我会使用File::Find::Rule 而不是 File::Find;它更简洁。)

my @dir_qfns = ( 'ram1', 'ram2' );

for my $dir_qfn (@dir_qfns) {
   for my $fn ('pass.txt', 'fail.txt') {
      my $file_qfn = "$dir_qfn/$fn";
      open(my $fh, '<', $file_qfn)
         or warn("Can't open \"$file_qfn\": $!\n"), next;

      defined( my $first_line = <$fh> )
         or warn("\"$file_qfn\" is empty\n"), next;

      print("$file_qfn: $first_line");
   }
}

If it's ok for a file to be missing, then you can ignore that error ( ENOENT ).如果文件丢失没有问题,那么您可以忽略该错误( ENOENT )。

Similarly, you don't need to output an error message if the file is empty.同样,如果文件为空,则不需要输出错误消息。

my @dir_qfns = ( 'ram1', 'ram2' );

for my $dir_qfn (@dir_qfns) {
   for my $fn ('pass.txt', 'fail.txt') {
      my $file_qfn = "$dir_qfn/$fn";

      my $fh;
      if (!open($fh, '<', $file_qfn)) {
         warn("Can't open \"$file_qfn\": $!\n") if $!{ENOENT};
         next;
      }

      defined( my $first_line = <$fh> )
         or next;

      print("$file_qfn: $first_line");
   }
}
if (open my $f, '<', 'pass.txt') { 
  my $firstLine = <$f>; 
  close $f;
}

OP's code does not make much sense. OP的代码没有多大意义。 Perhaps OP is looking for something of next kind也许 OP 正在寻找下一种东西

use strict;
use warnings;
use feature 'say';

my $dir   = shift || 'some_dir_to_start_from';
my @files = qw/pass.txt fail.txt/;
my $match = join '|', @files;
my $regex = qr/\b($match)\b/;

files_lookup($dir,$regex);

exit 0;

sub files_lookup {
    my $dir = shift;
    my $re  = shift;
    
    for ( glob("$dir/*") ) {
        files_lookup($_) if -d;

        next unless /$re/;
        
        if( -f ) {
            say "File: $_";

            open my $fh, '<', $_
                or die "Couldn't open $_";
            
            my $line = <$fh>;
            say $line;
            
            close $fh;
        }       
    }

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

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