43

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
6

6 Answers 6

59

So, the answer becomes:

column -t file_name

Note that this splits columns at any whitespace, not just tabs. If you want to split on tabs only, use:

column -t -s $'\t' -n file_name

The -s $'\t' sets the delimiter to tabs only and -n preserves empty columns (adjacent tabs).

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.

6
  • 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. Feb 20, 2011 at 19:23
  • Sure! I too was unaware of column :)
    – Barun
    Feb 21, 2011 at 5:51
  • 1
    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. Jul 18, 2012 at 7:29
  • 1
    Also, this command will not only split on tabs, but also on "any whitespace". To split just on tabs, use column -t -s $'\t'.
    – Fritz
    Feb 6, 2017 at 12:45
  • 1
    The $ before a single quote ' expands escape sequences in the following string. In this case '\t' is a string of two characters: bachslash (ASCII 0x5C) followed by t (ASCII 0x74). But $'\t' contains only one character: a Tab (ASCII 0x09), because the escape sequence \t (which stands for "tab") is expanded to an actual tab character. You can try this using hexdump: Try echo -n '\t' | hd vs. echo -n $'\t' | hd.
    – Fritz
    Dec 15, 2020 at 11:05
4

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, thanks to util-linux.)

3

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
4
  • Um, thanks, but I was hoping there's a more "portable" way to do that. Feb 20, 2011 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, 2011 at 11:13
  • 1
    it's as simple as column Feb 20, 2011 at 11:51
  • 2
    @Elzar Excellent! column -t -s $'\t' seems to do the job.
    – Mikel
    Feb 20, 2011 at 20:33
2

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
0
sed 's/||/| |/g;s/||/| |/g' filename-here | column -s"|" -t | less -#2 -N -S

Explanation:

Sed will add a space between blank delimters

Column will add equal spacing between the columns

zydsld|asl|asd
das|aosdk|dd

becomes

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

1
  • 1
    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, 2014 at 17:57
0

With Miller (http://johnkerl.org/miller/doc) you have pretty print output.

Run

mlr --inidx --ifs "\t" --opprint cat input | tail -n +2

to have

var1                                     var2 var3
var_with_long_name_which_ruins_alignment var2 var3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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