I get a lot of mileage out of
command | ruby -e 'ARGF.each { |line| #do stuff with line }' command | ruby -e 'ARGF.each { |line| #do stuff with line }' ruby_code = ARGV[0]
loop do
line = $stdin.gets()
l = line
break unless line
line.chomp!
final_ruby_code = 'puts "' + ruby_code + '"'
eval(final_ruby_code, binding())
end
Invoked like this: echo foo | rg "The double do: #{l * 2}"
To print: The double do: foofoo
Extremely useful when data munging around. #!/usr/bin/env ruby
if ARGV.size != 1
puts "USAGE: ruby-each-line CODE"
exit
end
STDIN.each_line do |line|
line = line.strip
l = line
eval(ARGV.first)
end ps ax | ruby -e 'ARGF.each { |line| puts line.split.first if line =~ /top/ }'
becomes: ps ax | ruby -ne 'puts $_.split.first if $_ =~ /top/'
It even squashes the extra newline the ARGF version prints out first. ps ax | ruby -ane 'puts $F[0] if /top/'