- gustavo's personal blog

Manipulating text from the command line with Ruby


sed, awk and grep are powerful tools to manipulate text, but they all have a learning curve that you may not be willing to take. If you already know Ruby, you may use it to run some basic text manipulation tasks. Piping the text to ruby -ne [CODE] will put each input line in the special variable $_, which you may process as needed.

Below you’ll find a stupid example where we pipe the output of ls and we get the absolute path of each file using Ruby:

gustavo@possantinho Vitamins $ ls | ruby -ne 'puts File.absolute_path($_)'