Adds Ruby's power to the command line.
Sometimes we need to process files or streams at the bash prompt for filtering, parsing, calculating etc. Unix offers many tools for doing those actions: grep, sed, awk etc. However, their usage are not easy to remember besause of their cryptic syntax. They also use Unix regexes which are more limited than Ruby's ones.
The Ruby interpreter offers us some command line options for processing files or pipes. The -p, -n options in combination of the -e option, allows us to process lines one at a time. Personnaly I never remember how to use them.
For this reason, I have created Rubyc, which stands for Ruby Command line.
gem install rubyc
Rubys supports some Enumerable methods applied to each line to the stdin stream.
Rubyc gives you the line variable and ıts shorter alias l to represent the current line on which Rubyc is iterating.
The index and lnum variables are also available to you. The index variable represents the index of the line starting at 0 whereas lnum is the line number which starts at 1.
Upcase what is comming from stdin.
The awk way:
$ ls | awk '{print toupper($0)}'
The Ruby interpreter with options way:
$ ls | ruby -pe '$_ = $_.upcase'
The Rubyc way:
ls | rubyc map 'line.upcase'
Extract columns 2 and 3 of a csv file that has columns containing commas. Note that, in that case the "cut" shell command does not work.
The shell way
$ ???
The Ruby interpreter with options way:
$ cat file1.csv | ruby -r csv -pe 'csv = CSV.parse_line($_); $_ = [csv[1], csv[2]].to_s + "\n"'
The Rubyc way:
$ cat file1.csv | rubyc map -r csv 'csv = CSV.parse_line(l); [csv[1], csv[2]]'
NOTE: -r is an alias for the --require= option indicates the gems needed for the exectution of the script. Note that for multiple require, gem names must me separated with a colon ':'.
The shell way:
$ rake 2> >(while read line;do echo -e "\033[31m$line\033[0m";done)
The Rubyc way:
$ rake 2> >(rubyc map -r colorize 'l.red')
Extract the number of insertions per table in a rails log file.
The shell way:
$ cat development.log | grep "INSERT INTO" | sed 's/\(.*\)INSERT INTO..\([a-z]*\).*/ \2 /' | sort | uniq -c
The Rubyc way:
$ cat development.log | rubyc count_by 'l =~ /INSERT INTO \"(\w+)\"/; $1'
---
users: 14
adresses: 18
objects: 102191
phone_numbers: 47
You can see all the currently supported methods by calling rubyc's help:
$ rubyc help
Tasks:
rubyc compact # Remove empty lines
rubyc count_by BLOCK # Count the number of lines that have the same property. The property is defined by the return value of the gi...
rubyc grep BLOCK # Enumerable#grep
rubyc help [TASK] # Describe available tasks or one specific task
rubyc map BLOCK # Enumerable#map
rubyc merge NB_LINES [SEPARATOR] # Merge NB_LINES consecutive lines using SEPARATOR. If SEPARATOR is not given ',' is used
rubyc reject BLOCK # Enumerable#reject
rubyc scan MATCHER BLOCK # String#scan
rubyc select BLOCK # Enumerable#select
rubyc sort_by BLOCK # Enumerable#sort_by
rubyc sum BLOCK # Active Support Enumerable#sum
rubyc uniq # Enumerable#uniq
Options:
-r, [--require=REQUIRE]