Bryan Harris wrote:

> 
> I'm interested in doing the equivalent of a horizontal "cat" on a bunch of
> files.  That is, instead of listing them one after another, I'd like them
> "cat"ted next to each other horizontally (tab separated).
> 
> 
> % hcat t1 t2 t3
> 0  10  15  20
> 1  11      21
> 2  12
>    13
>    14
> 
> I'd like it to be able to work on any number of files, and they might be
> huge (20+ MB).  I was thinking it'd be good to open up all the files at
> once, and write out the new file one line at a time, but I can't figure
> out how to set up an array of filehandles.  I'm not sure if that's the
> easiest way...
> 

you could open the file one by one, store its file handle, iterate through 
the file handle and print the line one by one:

#!/usr/bin/perl -w
use strict;

use Symbol;

my @fds = ();

for(@ARGV){
        my $fd = Symbol::gensym();
        open($fd,$_) || die $!;
        push(@fds,$fd);
}

while(1){

        my $finish = 0;

        for(@fds){
                my $line = <$_>;
                unless(defined $line){
                        $line = '';
                        $finish++;
                }
                chomp($line);
                substr($line,15) = '' if(length($line)>15);
                $line .=' 'x(15-length($line)) if(length($line)<15);
                print $line," ";
        }
        print "\n";

        last if($finish == $#fds);
}

close($_) for(@ARGV);

__END__

given:

file1.txt:
file1 line1
file1 line2 longer than usual
file1 line3 shorter

file2.txt:
file2 line1
file2 line2
file2 line3
file2 line4
file2 line5

file3.txt:
file3 line1

the script prints:

file1 line1     file2 line1     file3 line1
file1 line2 lon file2 line2
file1 line3 sho file2 line3
                file2 line4

i put in some simple formatting code to help you see the result better.

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to