Skip to content

Instantly share code, notes, and snippets.

@mw44118
Last active November 24, 2015 19:04
Show Gist options
  • Save mw44118/50b438d2d3d75624b7a2 to your computer and use it in GitHub Desktop.
Save mw44118/50b438d2d3d75624b7a2 to your computer and use it in GitHub Desktop.
View a tab-delimited text file in your terminal
column -s' ' -n -t datafile.txt | less -S
@mw44118
Copy link
Author

mw44118 commented Nov 24, 2015

Say you just downloaded a tab-delimited data file named datafile.txt. You want to view it in your terminal, but it's a headache to figure out what columns go with what headers.

Before you reach for excel, try out this tomfoolery first:

column -s'    ' -n -t datafile.txt | less -S

Replace datafile.txt with the name of your file.

This is really two programs, where the output of the first one, column is sent as input to the next program, less.

This is a really popular thing to do in unix.

The first program

column -s'    ' -n -t datafile.txt

The stuff in between the single-quotes and after -s is a literal tab character.

Here's a puzzle: since just about everybody has tab-completion turned on, so when you hit tab, you won't get a tab key. So how do you put a tab character into your terminal?

There's a bajillion ways to do that, but one of my favorites is to hit CTRL+V first. And then you can hit tab and you'll get a literal tab key.

Here's another way to get the same effect:

column -s$'\t' -n -t datafile.txt | less -S

The $'\t' value passed in to the -s gets interpreted by bash (maybe other shells, I don't know) as a tab key.

The -n option means don't combine numerous empty columns together. Incidentally, -n only exists with the GNU version of the column program, not the BSD version.

The -t option tells column that we want a table with all the columns arranged nicely.

The second part

Last, we send the result from column over to good old less -S. The less program is a pager, which means it lets you explore files. Or, in this case, streams of data that emulate files. The -S option says DO NOT WRAP ROWS! Instead, you can use your arrow keys to scroll right. Or if you like to pretend your keyboard doesn't have arrow keys, or maybe they're just too far out of reach, use those. I don't care. It's a free country. At least for now.

I hope this helps somebody out! If you've got tips, add them to the comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment