0

I am running a Perl script in windows ,which creates multiple text files with some info. I would like some help with a Perl script to put all the text file data into one file side by side. I know I could use nested foreach loop to parse through each file and print them side by side. But the number of files varies. For example:

Input:

file1.txt:

AAA
BBB
CCC

file2.txt:

DDD
EEE
FFF

Output:

AAA  DDD
BBB  EEE
CCC  FFF

Thanks in advance

4

3 回答 3

1

paste如果您没有设置滚动您自己的 perl 脚本,则linux 工具也提供此功能。

于 2013-02-20T00:19:22.647 回答
0

关于什么 :

open my $fh, ">", "concat.txt" or die $!;
my $string = qx(
    @echo off;
    type %*.txt;
);
print $fh $string;

?

于 2013-02-19T22:24:12.003 回答
0

这应该这样做。

use strict;
use warnings;
use IO::File;

my @files = (q[a.txt], q[b.txt]);

my @fhs = ();

foreach my $file (@files)
{
    my $fh = new IO::File $file, O_RDONLY;
    push @fhs, $fh if defined $fh;
}

while(1)
{
    my @lines = map { $_->getline } @fhs;

    last if grep { not defined $_ } @lines[0..(@fhs-1)];

    print join(qq[\t], map { s/[\r?\n]+/ /g; $_ } @lines) . qq[\r\n];
}

map { $_->close } @fhs;
于 2013-02-20T00:08:12.417 回答