How to use Ruby instead of sed and awk

Many unix utilities like sed, awk and grep
provide powerful ways to manipulate text.
But I always need to dig through the man pages and tutorials
before I can do anything with them.

This morning, I needed to remove all the empty lines from a text file.
Searching for ways to do this using unix tools turned up a few options:

awk 'NF' input.txt
sed -i'/^$/d' input.txt
grep-v'^$' input.txt

Remembering how to use these tools is always a challenge,
so I decided to look at how to do this in Ruby.
Ruby allows us to pass one-liner scripts from the command line,
which lets us use it in the same way we would use awk.

Before we try replacing sed or awk with Ruby,
let’s look at how we can run
simple Ruby one-liners from the command line.
For example:

$ ruby -e'puts 42'
42

Running this prints “42” to the console,
as you might have guessed.
The -e flag tells Ruby to read the script
from the command line,
and therefore executes puts 42.

Next, let’s look that the -n flag
which lets you pipe in text to Ruby,
and execute some code for each line of text.

$ echo'foo' | ruby -n-e'puts $_.upcase'
FOO

$_ is a special variable that contains the last line read from STDIN.
In this case, it prints out ‘FOO’.
This also works with multiple lines of input.
Say we have a file foo.txt
with the words foo, bar and baz on each line:

The BEGIN block is executed before it starts processing the lines,
so we initialize a global variable to contain the lines.
The $x << $_.chomp line adds each line to the array.
The END block is executed after all lines have been processed.

Now, let’s look at the -a flag
that splits the input and stores it in
a variable $F.
If we put the following text in a file:

Links

Hi, I’m Nithin Bekal.
I work at Shopify in Ottawa, Canada.
Previously, co-founder of
CrowdStudio.in and
WowMakers.
Ruby is my preferred programming language,
and the topic of most of my articles here,
but I'm also a big fan of Elixir.
Tweet to me at @nithinbekal.