Created
June 14, 2010 13:32
-
-
Save mikiobraun/437677 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- ruby -*- | |
# Usage: | |
# ruby printjson.rb field1 fields2 file | |
# | |
# if file == '-' then read from stdin | |
# | |
# Install the json gem first: | |
# | |
# gem install json | |
# | |
# Build your own console twitter-wall with | |
# | |
# curl -s http://stream.twitter.com/1/statuses/filter.json?track=<terms-to-track> -u<username>:<password> | ruby printjson.rb created_at text - | |
require 'rubygems' | |
require 'json' | |
fields = ARGV[0...-1] | |
file = ARGV[-1] | |
def print_field(json, fields) | |
fields = fields.split('.') | |
for f in fields | |
json = json[f] | |
end | |
return json | |
end | |
def print_json(line, fields) | |
begin | |
json = JSON.parse(line) | |
puts fields.map {|field| print_field(json, field)}.join "\t" | |
rescue JSON::ParserError | |
end | |
end | |
if file == '-' | |
$stdin.each_line do |line| print_json(line, fields) end | |
else | |
open(file).each_line do |line| print_json(line, fields) end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment