awkmultiple-columns

how to output data from multiple files in side by side columns in one file via awk?


I have 30 files, called UE1.dat, UE2.dat .... with 4 columns in every of them. An example of their column structure is given below for UE1.dat and UE2.dat.

UE1.dat

1 4 2 1 
2 2 3 3
3 2 4 4   
4 4 4 2

UE2.dat

2 6 8 7 
4 4 9 6
7 1 1 2   
9 3 3 3

So, i have tried with the following code:

for((i=1;i<=30;i++)); do awk 'NR$i {printf $1",";next} 1; END {print ""}' UE$i.dat; done > UE_all.dat

to get only the first column from every file and write them in a single file and columns to be side by side,The desired OUTPUT is given below.

1 2
2 4
3 7
4 9

But unfortunately, the code orders them in rows, can you give a hint?

Thank you in advance!


Solution

  • I'd probably go with something like - using perl rather than awk because I prefer the handling of data structures. In this case - we use a two dimensional array, insert the first column of each file into a new column of the array, then print the whole thing.

    #!/usr/bin/env perl
    use strict;
    use warnings;
    use Data::Dumper;
    
    my $num_files = 2; 
    
    my @rows;
    my $count = 0; 
    my $max = 0; 
    
    for my $filenum ( 1..$num_files ) {
        open ( my $input, "<", "UE${filenum}.dat" ) or die $!;
        while ( <$input> ) {
            my @fields = split;
            push ( @{$rows[$filenum]}, $fields[0] );
            $count++;
        } 
        close ( $input ); 
        if ( $count > $max ) { $max = $count };
    }
    
    print Dumper \@rows;
    
    for ( 0..$count ) { 
        foreach my $filenum ( 1..$num_files ) {
           print shift @{$rows[$filenum]} || ''," ";
        }
        print "\n";
    }