Shell filter in ruby

Hello,

Tired to use thinks like “awk ‘print $2’” ?

I have done a very litle tool in one ruby script which can make some
filter action with ruby code. Here the help :

===
Usage:

cat ddd | rfilter ‘expression’
or
rfilter ‘expression’ *.c

With
expression = sum(arg)|mult(arg)|toa(arg)|toai(arg)|
toh(a,b)|sela {}|sell {}
arg can be : _0 (all line), _1 (first word), _2 … or any ruby
data
_n words are result of splitting each line with /\s+/

At the end , current result is printed, if exist

Example:
select input with condition and do summation:

du . | rfilter ‘sell {_1.to_i>1000}’ | rfilter ‘sum _1’

LOC count:

ls .rb | xargs -i cat {} | rfilter 'sell {_0!=/^\s#/ && _0.size>0}’
| rfilter ‘sum 1’

Special filter:
cut(fields_number) : print stdin, as array, only indexed fields :
>du . | rfilter ‘cut 1,0’ # invers order : dir, size

I don’t know if think like that already exist, and if it can interest
someone…

so i found this so useful…
gist at :
https://gist.github.com/glurp/33775db6e47564975998

help:
Exemples:

Count volumes of files which have size bigger than 1K:

ls -l | rfilter ‘sell {_5.to_i>1024}’ | rfilter ‘sum _5’

Count LOC of shell-script files

find . -type f -name ‘.sh’ -exec cat {} ; |
rfilter 'sell {_0 !~ /^\s
#/ && _0.size>0}’ | wc -l

Format lines

ls -l | rfilter “format(’%-15s %10s’,_9,_5)”

Format lines if condition

ls -l a*.rb | rfilter “format_if(’%15s | %10s’,_9,_5) { _5.to_i>1}”
a.rb | 864
abool.rb | 888
allways.rb | 698
anac.rb | 5777

Calculator :

echo 10.33 22.44 | rfilter ‘puts Math.sin(_1.to_f) ; puts _2.to_f*2’
-0.7865622859965424
44.88

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs