I have no idea what this is doing, but it sure looks interesting.
It's using Ruby to replace Awk, the way Perl once tried to.
Show HN: Rb – Turns Ruby into a command line utility
21–30 of 79 posts
Re: Show HN: Rb – Turns Ruby into a command line utility
#22Re: Show HN: Rb – Turns Ruby into a command line utility
#23Re: Show HN: Rb – Turns Ruby into a command line utility
#24Interesting, so it's reading text from STDIN into an Array, then executing its arguments as Ruby code in the context of that Array (or if you pass -l, in the context of each line individually). So all the standard methods on the [Array][1] (or [String][2]) class become accessible as top-level methods. A few examples: Capitalizing a line: echo hello world | rb -l capitalize Printing only unique lines: printf 'hello\nw…
echo hello world | ruby -pe '$_.capitalize!' printf 'hello\nworld\nhello\n' | ruby -le 'puts STDIN.to_a.uniq ps | ruby -lane 'BEGIN { b = 0 }; b += $F[0].to_i; END { print "sum of PIDs: #{b}" } select whatever column you want, to sum up. Really what Ruby needs is a flag that adds some capabilities to NilClass, so that these BEGIN blocks aren't needed.
Does it, though?
ps | ruby -le 'b = $
or: ps | ruby -lane 'b = (b || 0) + $F[0].to_i; END { print "sum of PIDs: #{b}" }'Re: Show HN: Rb – Turns Ruby into a command line utility
#25 $ docker ps | rb drop 1 | rb -l split[1]
$ docker ps | perl -anE 'say $F[1] if $.>1'
perl solved this problem a long time ago, peopleRe: Show HN: Rb – Turns Ruby into a command line utility
#26Command line tools in 10 lines of ruby (using this script): docker ps | rb drop 1 | rb -l split[1] docker ps -a | rb grep /Exited/ | rb -l 'split.last.ljust(20) + " => " + split(/ {2,}/)[-2]' df -h | rb 'drop(1).sort_by { |l| l.split[-2].to_f }' Command line tools in zero lines of ruby (using ruby): docker ps | ruby -lane 'next if $. == 1; print $F[1] docker ps -a | ruby -lne 'print $1 if /(Exited .*? ago)/' df | rub…
df -h | ruby -e 'puts readlines.drop(1).sort_by { |l| l.split[-2].to_f }'
similarly, the `uniq` example from your other comment could be shortened to printf 'hello\nworld\nhello\n' | ruby -e 'puts readlines.uniq'Re: Show HN: Rb – Turns Ruby into a command line utility
#27$ docker ps | rb drop 1 | rb -l split[1] $ docker ps | perl -anE 'say $F[1] if $.>1' perl solved this problem a long time ago, people
awk 'NR>1{print $2}'
or ruby[1] ruby -ane 'puts $F[1] if $.>1'
which one to use depends on lots of factor - speed, features, availability, etc as well as whether user already knows perl[2]/ruby/etcFurther reading(disclosure: I wrote these)
[0]: https://github.com/learnbyexample/Command-line-text-processi...
[1]: https://github.com/learnbyexample/Command-line-text-processi...
[2]: https://github.com/learnbyexample/Command-line-text-processi...
Re: Show HN: Rb – Turns Ruby into a command line utility
#28I though it was great, knowing python better than bash, plus it was portable to windows.
But eventually I always end up using built in GNU tools.
Don't know why. It just rolls better.
Re: Show HN: Rb – Turns Ruby into a command line utility
#29Earlier quoted context omitted.
Out of interest, why are you not a fan of Ruby?
Right tool for the job; don't be a fan boy.
Re: Show HN: Rb – Turns Ruby into a command line utility
#30$ docker ps | rb drop 1 | rb -l split[1] $ docker ps | perl -anE 'say $F[1] if $.>1' perl solved this problem a long time ago, people