Skip to content

Instantly share code, notes, and snippets.

@thbar
Created April 19, 2012 19:54
Show Gist options
  • Select an option

  • Save thbar/2423735 to your computer and use it in GitHub Desktop.

Select an option

Save thbar/2423735 to your computer and use it in GitHub Desktop.
Ruby CSV pipe pretty-printer

To extract line 25706 from a csv file and pretty-print it:

sed -n "1p;25706p" MyFile.csv | csv

#!/usr/bin/env ruby
require 'rubygems'
require 'bundler'
Bundler.setup
require 'csv'
require 'awesome_print'
if STDIN.tty?
puts 'your_command | csv'
else
CSV.parse(STDIN.read, :headers => true) do |row|
ap row.to_hash
end
end
@undx

undx commented Apr 19, 2012

Copy link
Copy Markdown

require 'awesome_print' instead 'ap' no ?

@thbar

thbar commented Apr 20, 2012

Copy link
Copy Markdown
Author

Good call - the 'ap' one is legacy from before the gem changed name. Updating!

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