command to layout tab separated list nicely

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.)

Method 3

Here’s a script to do it:

aligntabs.pl

#!/usr/bin/perl

my $delim = 's*ts*';

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

Method 4

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

Method 5

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

Method 6

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x