简体   繁体   English

使用Perl从2个文件中逐行打开和读取

[英]Opening and Reading line by line from 2 files using Perl

I'm trying to print line by line of 2 different files in Perl alternately. 我试图在Perl中逐行打印2个不同文件。 Whats the syntax for this ? 这是什么语法? Code: 码:

#1. Initialize: log file path and ref Log file path
use strict;
use warnings;
my $flagRef="true";
my $logPath="C:/ExecutionSDKTest_10.2.2/Logs/${ARGV[0]}.log";
my $refLogPath="C:/ExecutionSDKTest_10.2.2/Logs/${ARGV[1]}_Ref.properties.txt";
#print log file path followed by reflog path: P
#print "$logPath\n$refLogPath\n";

#2. Traverse log file and refLog file to replace ALL instances of:
#"hr:min:sec" with "xx:xx:xx" 
open INLOG, $logPath or die $!;
open INREF, $refLogPath or die $!;

#equiv: >>$logLine=readline(*FHLOG);        
#$logLine= <FHLOG>;
#$refLine= <FHREF>;
while (<INLOG>) #syntax
{
   print "$_";
   #I'd like to print lines from INREF too! :)
   #Syntax?
 }

This can be solved easily using paste(1). 使用paste(1)可以轻松解决。 Say a.txt contains: 说a.txt包含:

a1
a2
a3

and b.txt contains: 并且b.txt包含:

b1
b2
b3

Then: 然后:

$ paste -d '\n' a.txt b.txt
a1
b1
a2
b2
a3
b3
$ 

A slightly long Perl one-liner to do the same: 稍长的Perl一线式可做相同的事情:

$ perl -e '@fhs=map { open my $fh, "<", $_; $fh } @ARGV; while (!$done) { $done=1; map { $v=<$_>; do { print $v; $done=0 } if (defined $v); } @fhs; }' a.txt b.txt
a1
b1
a2
b2
a3
b3
$ 

Unfolding it and rewriting it into a proper script is left as an exercise for the reader. 展开并将其重写为适当的脚本,作为读者的练习。

Here is a simple script that'll do what you are asking: 这是一个简单的脚本,可以满足您的要求:

#!/usr/bin/perl
use strict;

open FILE1,'<file1';
open FILE2,'<file2';

my @arr1 = <FILE1>;
my @arr2 = <FILE2>;

if(@arr1 > @arr2) {
    print_arrs(\@arr1,\@arr2);
}
else {
    print_arrs(\@arr2,\@arr1);
}

sub print_arrs {
    my ($arr1,$arr2) = @_;
    for(my $k = 0; $k < @{$arr1}; $k++) {
        print @{$arr1}[$k];
        print @{$arr2}[$k] if @{$arr2}[$k];
    }
}

It may not be the most elegant solution, but it does what you're asking for! 它可能不是最优雅的解决方案,但是它可以满足您的要求!

It is generally better practice to process files in a while loop rather than reading them into memory in their entirety. 通常,更好的做法是在while循环中处理文件,而不是将文件全部读取到内存中。

This program alternates lines from the two files as you requested. 该程序根据您的要求在两个文件中交替显示行。

use strict;
use warnings;

my $logPath = sprintf 'C:/ExecutionSDKTest_10.2.2/Logs/%s.log', shift;
my $refLogPath = sprintf 'C:/ExecutionSDKTest_10.2.2/Logs/%s_Ref.properties.txt', shift;

open my $inlog, '<', $logPath or die qq(Unable to open "$logPath": $!);
open my $inref, '<', $refLogPath or die qq(Unable to open "$refLogPath": $!);

do {
  print if defined($_ = <$inlog>);
  print if defined($_ = <$inref>);
} until eof $inlog and eof $inref;

Another way that is more like the way used in the question would be: 与问题中使用的方式更相似的另一种方式是:

use strict;
use warnings;

open( my $h1, '<', 'a.txt' );
open( my $h2, '<', 'b.txt' );

for(;;)
{
    my $x    = <$h1>;
    my $y    = <$h2>;
    defined($x) || defined($y)
        or last;
    print $x // '';
    print $y // '';
}

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

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