Skip to content

Instantly share code, notes, and snippets.

@ryenus
Created December 25, 2011 01:15
Show Gist options
  • Save ryenus/1518596 to your computer and use it in GitHub Desktop.
Save ryenus/1518596 to your computer and use it in GitHub Desktop.
Using ruby in place of grep/awk/sed like perl

With options -e, -n, -p, perl can do what grep/awk/sed can, what about ruby?

Let's take the result of ls -l as input and process it with ruby

grep with ruby

\ls -l | ruby -ne 'print if /^d/'

awk with ruby

\ls -l | ruby -ne 'puts split(/\s+/).last if /^d/'

sed with ruby

\ls -l | ruby -pe 'gsub /.*\s+(\S+)$/, %q|\1|'
\ls -l | ruby -pe 'gsub(/.*\s+(\S+)$/) {$1.capitalize}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment