3 ms·
Command 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.
by star-techate 8y ago
Command 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 | ruby -lane 'BEGIN { lines = [] }; lines.push [$F[4].to_i, $_] if $. > 0; END { lines.sort { |a,b|b[0] <=> a[0] }.each{|k| print k[1] } }
Bit of a pain as ++ is lacking, as is autovivication. a /bin/sort at the end usually beats `<=>` for terseness.
- shawn 8y agoIn case it’s helpful, as someone who doesn't know ruby, it's hard to understand. Not too readable. Split.last.ljust I just barely understood (left-justify the last match), but I have no idea what the next part is even trying to accomplish. It's a cool script though.
- star-techate 8y agoThat's the author's oneliner. I don't know what that code is for either, which is part of I rewrote it in the second set of examples. It's normal for a oneliner to be somewhat inscrutable though, since they're often extremely dependent on their inputs. Like if it's important to fetch the 3rd, 5th, and 7th column--why? You can't tell without the input.
- asicsp 8y agofor the 3rd example, not sure why you are manually building the array.. 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'