Skip to content

Instantly share code, notes, and snippets.

@jclay
Last active February 13, 2019 02:00
Show Gist options
  • Save jclay/6618e0fd4223e415a244d810d0d921f3 to your computer and use it in GitHub Desktop.
Save jclay/6618e0fd4223e415a244d810d0d921f3 to your computer and use it in GitHub Desktop.
Pretty print table from stdout

Pretty printing a table from stdout

On a recent project, I was outputting some tab separated data to stdout for debugging. It looked something like this:

Type	Result	Time (ms)	
----	----	----	
Traditional	54986	0.001842	
Range	54986	0.000325	
For Each	54986	0.000282	

Using the column command, we can turn this into something much easier to read:

❯ ./measurements | column -t -s $'\t'
Type         Result  Time (ms)
----         ----    ----
Traditional  56085   0.00169
Range        56085   0.00032
For Each     56085   0.000257

Much better!

The -t tells column we are working with tabulated data and to assume some defaults. -s $'\t' specifies that we only want to treat tab as a column separator.

We can also sort these results, but I have to strip the headers from the data to do so:

❯ ./measurements | tail -n +3 | column -tds $'\t' | sort -nk3 -t $'\t'
For Each     48140  0.000277
Range        48140  0.000326
Traditional  48140  0.002078
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment