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($_)'
/home/gustavo/Projects/Vitamins/app
/home/gustavo/Projects/Vitamins/bin
/home/gustavo/Projects/Vitamins/composer.json
/home/gustavo/Projects/Vitamins/composer.lock
/home/gustavo/Projects/Vitamins/src
/home/gustavo/Projects/Vitamins/vendor
/home/gustavo/Projects/Vitamins/web

I used to have Disqus enabled on my website, but I have disabled it because of privacy concerns.

If you feel like commenting or asking something, you can still contact me via other means.