Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I work with a lot of tab-delimited data files, with varying columns of uncertain length.

Typically, the way people view these files is to bring them down from the server to their Windows or Mac machine, and then open them up in Excel. This is certainly fully-featured, allowing filtering and other nice options. But sometimes, you just want to look at something quickly on the command line.

I wrote a bare-bones utility to display the first<n>lines of a file like so:

--- line 1 ---
1:{header-1} 2:{header-2} 3:...

--- line 2 ---
1:{data-1} 2:{data-2} 3:...

This is, obviously, very lame, but it's enough to pipe through grep, or figure out which header columns to use "cut -f" on.

Is there a *nix-based viewer for a terminal session which will display rows and columns of a tab-delimited file and let you move the viewing window over the file, or otherwise look at data?

I don't want to write this myself; instead, I'd just make a reformatter which would replace tabs with spaces for padding so I could open the file up in emacs and see aligned columns. But if there's already a tool out there to do something like this, that'd be great!

(Or, I could just live with Excel.)

share|improve this question
add comment

5 Answers

up vote 3 down vote accepted

There is a Unix text-mode spreadsheet called sc which may work for your use. You will probably need to pass the file through psc to convert it to the file format that sc uses.

share|improve this answer
    
That sounds very promising! It seems to have led me down the rabbit-hole of figuring out where to get sc, and why my Fedora installation isn't seeing all package upgrades, but that's not anyone else's problem. Thanks! –  khedron May 12 '10 at 18:48
    
That works very well, thank you! Especially for the psc suggestion. Now I just wish I knew how to pass tab as the single delimiter character to psc... –  weronika Jan 1 '12 at 3:33
    
@weronika: If -d '\t' doesn't work, try -d $'\t' –  Dennis Williamson Jan 1 '12 at 3:51
    
@Dennis: Yes, I figured that out eventually. :) Great solution, I use this all the time now. –  weronika Jan 28 '12 at 6:16
add comment

There is another solution, which involves a script. Save this script to, for example, tab.pl.

#!/usr/bin/perl

use strict;

my @lines;
# first read the file into a list of lists
while (<>)
{
    chomp; # remove the newline from the end of the line
    my @fields = split("\t");
    push @lines, \@fields;
}
my @lengths;
# calculate the maximum lengths of each field
foreach (@lines)
{
    for (my $i = 0; $i < scalar @$_; $i++)
    {
        $lengths[$i] = $lengths[$i] < length $$_[$i] ? length $$_[$i] : $lengths[$i];
    }
}
# now print the text aligned
foreach (@lines)
{
    for (my $i = 0; $i < scalar @$_; $i++)
    {
        print $$_[$i], " " x ($lengths[$i] - length ($$_[$i]) + 1);
    }
    print "\n";
}

Then just type:

perl tab.pl some_file.csv | less

Or save the result to a file and open it with your favourite text editor:

perl tab.pl some_file.csv > result.txt
share|improve this answer
    
Something like that is what I was thinking of when I said I could make a reformatter to replace tabs with spaces and pad. I will freely admit that your version is much more compact than mine would've been! Thank you! –  khedron May 12 '10 at 18:49
    
Very nice, thank you! I grabbed it, it works pretty well, though I'll have to modify it a bit to deal with the fact that my files tend to contain non-tab-separated headers/footers that should be dealt with separately. –  weronika Dec 30 '11 at 16:36
    
Thanks for saving me the trouble! –  Aaron Miller Jul 17 '13 at 16:10
add comment

This works to output a pretty print version of a tab delimited file

column -t -s $'\t' list-of-entries.txt
share|improve this answer
add comment

Open the file in vim and set tab stop to something high, so the columns will be lined up. If you are not familiar with vim, first start it up:

vim some_file.csv

Then set tab stop to some high value, so the columns will be lined up. Type:

:set tabstop=20

Should lines become too long, you should also turn off wrapping, so you can scroll sideways instead of wrapping the text around:

:set nowrap

Moving around: there are several ways, but the most straightforward ones like arrow keys and pgup/pgdown/home/end will work. Some other useful movement commands:

CTRL+U: move half screen up
CTRL+D: move half screen down
gg: move to top
G: move to bottom

You can search for text the same way you would do in man:

/regular_expression (search forward)
?regular_expression (search backward)

Then type "n" to find the next match, and "N" to find the previous one.

You can also navigate using the mouse if your terminal supports it, and you type:

:set mouse=a

Edit: I forgot that you can exit with:

:q (if you haven't changed the text)
:wq (to save it before quitting)
:q! (to abandon changes)
share|improve this answer
    
That worked pretty well -- until I got to the column that was somewhere between 40 and 200 characters. Spacing after that was confused, as you might imagine. But tab-settings + nowrap came close! I will have to remember that. –  khedron May 11 '10 at 20:38
    
Oh, awesome!! So simple. I can't believe I never thought of doing this when struggling with those tab-separated files in vi. –  weronika Dec 30 '11 at 16:29
add comment

pr allows you to expand tabs to a given number of spaces with -e. Don't forget to pass -t -T so that it doesn't actually format the whole page for printing.

share|improve this answer
add comment

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.