Question

File 1, 2 and 3 has many column, I need to extract specific column from specific files like first 3 column from file 1, 3rd column from file 2 and 3 rd column from file 3 and save it has another file. Note: File names are not fixed but the column to be extract are fixed

my @file = <> 
my @OA = ();

foreach my $line (@file)
{
        my @temp = split(/\t/, trim($_));
        push(@OA, $temp[0] . "\t" . $temp[1] . "\t" . $temp[2] . "\n");
}

This will help for extracting data from single file. can anyone help me to extend this to get the column from file 2 and so on.

Thanks, N.

Was it helpful?

Solution

Here's a stub of how you could interact with multiple file handles in parallel:

#!/usr/bin/perl 

use strict;
use warnings;
use autodie;

my @fhs;
for my $file (qw(file1.txt file2.txt file3.txt)) {
    open my $fh, '<', $file;
    push @fhs, $fh;
}

while (! grep eof, @fhs) {
    my @lines = map {scalar <$_>} @fhs;
    chomp @lines;

    print "fh0 = $lines[0]\n";
    print "fh1 = $lines[1]\n";
    print "fh2 = $lines[2]\n";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top