Skip to content

Instantly share code, notes, and snippets.

@ryangreenberg
Created March 30, 2017 16:31
Show Gist options
  • Save ryangreenberg/c0d971b78364b86958c8f24643acd720 to your computer and use it in GitHub Desktop.
Save ryangreenberg/c0d971b78364b86958c8f24643acd720 to your computer and use it in GitHub Desktop.
tableize: Turns tab-separated lines into Atlassian or Markdown formatted tables, with header.
#!/usr/bin/env ruby
# Turns tab-separated lines into Atlassian or Markdown formatted tables, with header.
#
# USAGE: copy some query results from SequelPro or some data out of a Google spredsheet, including header, then:
# > pbpaste | tableize | pbcopy
# paste in Jira.
#
#
# Atlassian Syntax:
# read from stdin, surround first line with double pipes and replace each tab with " || "
# read every other line, surround with pipe and replace each tab with " | "
#
# Markdown Syntax:
# Read from stdin, output pipe-separated headers
# Output separator
#
# Header | Header | Header
# -------|--------|-------
# Cell | Cell | Cell
# Cell | Cell | Cell
TAB = /\t/
WHITESPACE = /\s+/
SPLIT_ON = ARGV.include?("-w") ? WHITESPACE : TAB
FORMAT = ARGV.include?("-m") ? :markdown : :atlassian
def format_line(tsv, separator, split_on = /\t/)
str = tsv.chomp.
gsub(/\|/, "\\|").
gsub(split_on," #{separator} ")
"#{separator} #{str} #{separator}"
end
def main(args)
if FORMAT == :markdown
STDIN.each_line.with_index do |line, idx|
puts format_line(line, separator, split_on=SPLIT_ON)
end
else
STDIN.each_line.with_index do |line, idx|
separator = idx == 0 ? "||" : "|"
puts format_line(line, separator, split_on=SPLIT_ON)
end
end
end
main(ARGV)
@ryangreenberg
Copy link
Author

My most common usage of this command is pbpaste | tableize | pbcopy so I have a shortcut for that as well:

$ cat `which pbtableize`
#!/bin/bash

pbpaste | tableize | pbcopy

@ryangreenberg
Copy link
Author

Hat-tip to @mceachen from whom I originally copied this script.

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