brendano (owner)

Revisions

gist: 67656 Download_button fork
public
Description:
map and aggregate for shell
Public Clone URL: git://gist.github.com/67656.git
Embed All Files: show embed
mapagg #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env ruby
 
# map and aggregate for shell pipelines
 
# Two args: mapper, then agger
# mapper is evaluated within each line
# agger is evaluated within an array holding all mapper results
#
# do a sum
# cat numbers | mapagg to_i 'inject(0){|s,x| s+x}'
# find max value in column
# cat tsv | mapagg 'split("\t")[0].to_i' sort[0]
 
raise "exactly two args: mapper, then the agger. (or leave out agger)" unless ARGV.size >= 1
 
# for crazy nonsense like dotfiles.org/~brendano/.irbrc
if File.exists?("#{ENV['HOME']}/.irbrc")
  $irbrc_extensions_only = true
  load "~/.irbrc"
end
 
cmd = ARGV[0]
mapped = STDIN.map do |line|
  line.chomp.instance_eval { eval cmd }
end
 
cmd = ARGV[1] || "self"
result = mapped.instance_eval { eval cmd }
puts result