Skip to content

Instantly share code, notes, and snippets.

@shtakai
Forked from ryenus/gist:1518596
Created September 17, 2018 10:00
Show Gist options
  • Save shtakai/6cb590247d2a4c11f51758d5e5d9b9d4 to your computer and use it in GitHub Desktop.
Save shtakai/6cb590247d2a4c11f51758d5e5d9b9d4 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