Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
Tried piping it to column -t? – alex Feb 20 '11 at 11:45
Tucked away at the end of Mikel's perl answer is the clincher comment (by Mikel)... columns -t acts on general whitespace. To work with tabs only, use column -t -s $'\t' – Peter.O Feb 20 '11 at 23:40
up vote 28 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:



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";


$ 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
it's as simple as column – Elazar Leibovich Feb 20 '11 at 11:51
@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
sed 's/||/| |/g;s/||/| |/g' filename-here | column -s"|" -t | less -#2 -N -S


Sed will add a space between blank delimters

Column will add equal spacing between the columns



zydsld|asl  |asd
das   |aosdk|dd 

Less will open the output in a file viewer. -N and -S will add line number and disable wrapping respectively

share|improve this answer
One-line answers are often not the most helpful. Consider expanding your post to include explanation of your solution, or documentation that supports it. – HalosGhost Aug 6 '14 at 17:57

Following on from Peter.O's comment which is what I wanted to align (tab delimited data, TSV), this phrase works very nicely:

column -t -s $'\t' /Users/me/data.csv | less --chop-long-lines
share|improve this answer

Your Answer


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.