Skip to content

Instantly share code, notes, and snippets.

@m040601
Created October 29, 2010 10:40
Show Gist options
  • Save m040601/653312 to your computer and use it in GitHub Desktop.
Save m040601/653312 to your computer and use it in GitHub Desktop.
desc this gist - snippets yaml, csv, plain conversion and marshalling
#!/usr/bin/env ruby
#
# Originally written by http://redartisan.com/tags/csv
# Added and minor changes by Gavin Laking
# only works with ruby 1.8 ???
#
# "id","name","mime_type","extensions","icon_url"
# "1","unknown","unknown/unknown","||","/images/icon/file_unknown.gif"
# "2","image/tiff","image/tiff","|tiff|tif|","/images/icon/blank.png"
#
# if you want to remove the id: "number" line from the resulting YAML file
# do a find and replace for: ^( id: \"\d*\"\n) in Textmate
require 'csv'
class String
def unquote
self.gsub(/^"|"$/, '')
end
end
# first line contains the field names
line = gets
fields = line.split('","').collect {|f| f.unquote.chomp}
CSV::Reader.parse(STDIN) do |row|
fixture = "record_#{row[0]}:\n"
fields.each_with_index do |field, i|
fixture += " #{field}: \"#{row[i]}\"\n"
end
puts fixture; puts
end
# yaml to command line
#
#ruby -e 'require "yaml"; puts( [ "dogs", "cats", "mices"].to_yaml)'
# ruby command line using gems
ruby -e 'command'
ruby -rubygems -e 'command'
cat db/seeds/countries.json | ruby -rubygems -e 'require "json";a=JSON.pretty_generate JSON.parse gets;File.open("db/seeds/countries.json", "w"){|f| f << a}'
# sqlite, shell
#
sqlite3 -line database.sqlite 'select * from table;' > output_file.txt
# column
#
#Pretty Print a simple csv in the command line
#Splits the input based on commas and prints it in a nice column format.
#This would not work for CSV rows that have "," between quotes or with newline characters. Use only simple
#simple csv files.
#
column -s, -t <tmp.csv
# python, csv pretty print commmand line
#
#Will handle pretty much all types of CSV Files.
#The ^M character is typed on the command line using Ctrl-V Ctrl-M and can be replaced with any character that # does not appear inside the CSV.
#Tips for simpler CSV files:
# If newlines are not placed within a csv cell then you can replace `map(repr, r)` with r
#
python -c 'import sys,csv; c = csv.reader(sys.stdin); [sys.stdout.write("^M".join(map(repr,r))+"\n") for r in c];' <tmp/test.csv | column -s '^M' -t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment