I have a C program that outputs two columns, utterly misaligned. The reason for the misalignment is lengths of words in the first column are very different.
I have an o
I wrote a small program that solves this problem using Perl. It also works for multiple columns.
#!/usr/bin/perl
use strict;
use warnings;
my $sep = 2;
sub max {
my ($a,$b) = @_;
return $a > $b ? $a : $b;
}
my @rows;
my $cols;
my $max = 0;
while (<>) {
next if m/^\s*$/;
my (@cols) = split m'\s+';
for (@cols) {
$max = max($max, length);
}
$cols = @cols;
push @rows, \@cols;
}
for (@rows) {
my $str = join '', (('%-' . ($max+$sep) . 's') x $cols);
$str .= "\n";
printf $str, @$_;
}