Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Sometimes, I'm getting as an input tab separated list, which is not quite aligned, for instance

var1  var2  var3
var_with_long_name_which_ruins_alignment  var2 var3

Is there an easy way to render them aligned?

var1                                      var2  var3
var_with_long_name_which_ruins_alignment  var2  var3
share|improve this question
    
Someone could make a solution based on elastic tabstops: nickgravgaard.com/elastictabstops –  Mikel Feb 20 '11 at 9:24
    
    
And a Go implementation: golang.org/pkg/tabwriter –  Mikel Feb 20 '11 at 10:00
6  
Tried piping it to column -t? –  alex Feb 20 '11 at 11:45
    
@alex, that's what I was looking for. Please post it as an answer and I'll accept that. –  Elazar Leibovich Feb 20 '11 at 11:51

3 Answers 3

up vote 10 down vote accepted

So, the answer becomes:

column -t file_name

P.S.: Just want to point out that the credit goes to Alex as well. The original hint was provided by him as a comment to the question, but was never posted as an answer.

share|improve this answer
    
I'll wait a bit for Alex to get the credit, I think he deserves it. If he wouldn't answer in a few days I'll accept an answer from somebody else. –  Elazar Leibovich Feb 20 '11 at 19:23
    
Sure! I too was unaware of column :) –  Barun Feb 21 '11 at 5:51
    
This seems ideal but unfortunately column seems to fail when it encounters empty cells. See this post. Depending on which version of column you have, you may be able to specify the -n option to correct this. –  John J. Camilleri Jul 18 '12 at 7:29

Here's a script to do it:

aligntabs.pl

#!/usr/bin/perl

my $delim = '\s*\t\s*';

my %length = ();
my @lines = ();
for my $line (<>) {
    chomp $line;
    my @words = split $delim, $line;
    my $numwords = scalar(@words);
    for my $i (0..$numwords-1) {
        my $maxlen = $length{$i} // 0;
        my $thislen = length($words[$i]);
        $maxlen = ($thislen > $maxlen)? $thislen: $maxlen;
        $length{$i} = $maxlen;
    }
    push @lines, [@words];
}

foreach my $wordsref (@lines) {
    my @words = @$wordsref;
    my $numwords = scalar(@words);
    for my $i (0..$numwords-1) {
        if ($i < $numwords-1) {
            my $fieldlen = $length{$i};
            printf "%-${fieldlen}s ", $words[$i];
        }
        else {
            print $words[$i];
        }
    }
    print "\n";
}

usage

$ aligntabs.pl < infile
var1                                     var2 var3
var_with_long_name_which_ruins_alignment var2 var3
share|improve this answer
    
Um, thanks, but I was hoping there's a more "portable" way to do that. –  Elazar Leibovich Feb 20 '11 at 11:08
    
Me too! Couldn't find one. pr and nl are the two basic tools for the formatting, and after that awk, sed, perl, etc. –  Mikel Feb 20 '11 at 11:13
1  
it's as simple as column –  Elazar Leibovich Feb 20 '11 at 11:51
1  
@Elzar Excellent! column -t -s $'\t' seems to do the job. –  Mikel Feb 20 '11 at 20:33

For manual tab stops: expand -t 42,48

For automatic tab stops, as suggested by alex: column -t

(expand is on all POSIX systems. column is a BSD utility, available in many Linux distributions as well.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.