سؤال

I am trying to transpose a text file containing only a large matrix of numbers and has no delimiters at all. I am hoping for a relatively simple solution to this as I cannot figure this out, and am new to programming with large files.

Input file:

    01120154031
    22014141423
    21312221323

Desired Output

    022
    121
    103
    211
    042
    112
    542
    411
    043
    322
    133

Any help would be hugely appreciated

هل كانت مفيدة؟

المحلول

Here is a solution using awk:

awk -v FS="" '
{
    for(i=1; i<=NF; i++) {
        num[i] = (num[i] ? num[i] $i : $i==0 ? "0" : $i)
    }
}
END {
    for(i=1; i<=NF; i++) print num[i]
}' file
022
121
103
211
042
112
542
411
043
322
133

نصائح أخرى

Perl

#!/usr/bin/env perl
use strict;
use warnings;

my @lines = <>;

my $len = length($lines[0]);
for (my $i = 0; $i < $len; $i++)
{
    for (my $j = 0; $j < scalar(@lines); $j++)
    {
        print substr($lines[$j], $i, 1);
    }
    print "\n";
}

This simply assumes the lines are all the same length; if they're not, GIGO.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top