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

0

3 Answers 3

1

The linux tool paste also provides this functionality, if you're not set on rolling-your-own perl script.

Sign up to request clarification or add additional context in comments.

Comments

0

What about :

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

?

Comments

0

This should do it.

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.