The Ruby One-Liner Cookbook: 30+ Recipes for the Command Line
Every recipe shows the Ruby one-liner, its Perl equivalent, and what it does. Print this out, tape it to your monitor, and stop Googling the same awk command for the fifteenth time.Part 1: Print and Filter Lines
# Print lines matching a pattern (grep) ruby -ne 'puts $_ if /ERROR/i' file.txt # Perl: perl -ne 'print if /ERROR/i' file.txt # Print lines NOT matching a pattern (grep -v) ruby -ne 'puts $_ unless /DEBUG/' file.txt # Perl: perl -ne 'print unless /DEBUG/' file.txt # Print lines matching multiple patterns ruby -ne 'puts $_ if /ERROR/ || /WARN/' file.txt # Perl: perl -ne 'print if /ERROR/ || /WARN/' file.txt # Print lines between two patterns (flip-flop) ruby -ne 'print if /START/../END/' file.txt # Perl: perl -ne 'print if /START/../END/' file.txt # Print specific line numbers ruby -ne 'puts $_ if $. >= 5 && $. <= 10' file.txt # Perl: perl -ne 'print if $. >= 5 && $. <= 10' file.txt # Print first N lines (head) ruby -ne 'puts $_ if $. <= 10' file.txt # Perl: perl -ne 'print if $. <= 10' file.txt # Print last line ruby -ne 'END { puts $_ }' file.txt # Perl: perl -ne 'END { print }' file.txt
Part 2: Count and Tally
# Count lines matching a pattern ruby -ne 'BEGIN{$n=0}; $n+=1 if /error/i; END{puts $n}' file.txt # Perl: perl -ne '$n++ if /error/i; END{print "$n\n"}' file.txt # Count total lines ruby -ne 'END { puts $. }' file.txt # Perl: perl -ne 'END { print "$.\n" }' file.txt # Word frequency count ruby -ane 'BEGIN{$h=Hash.new(0)}; $F.each{|w| $h[w.downcase]+=1}; END{$h.sort_by{|k,v| -v}.each{|k,v| puts "#{v}\t#{k}"}}' file.txt # Unique lines (like sort -u but preserves order) ruby -ne 'BEGIN{$s=Set.new; require "set"}; puts $_ if $s.add?($_.chomp)' file.txt
Part 3: Text Transformation
# Convert to uppercase ruby -pe '$_.upcase!' file.txt # Perl: perl -pe '$_ = uc' file.txt # Convert to lowercase ruby -pe '$_.downcase!' file.txt # Perl: perl -pe '$_ = lc' file.txt # Strip leading/trailing whitespace ruby -pe '$_.strip!' file.txt # Perl: perl -pe 's/^\s+|\s+$//g' file.txt # Squeeze multiple spaces to one ruby -pe 'gsub(%r~\s+~, " ")' file.txt # Perl: perl -pe 's/\s+/ /g' file.txt # Reverse each line ruby -lne 'puts $_.reverse' file.txt # Perl: perl -lne 'print scalar reverse' file.txt # Remove blank lines ruby -ne 'puts $_ unless $_.strip.empty?' file.txt # Perl: perl -ne 'print unless /^\s*$/' file.txt
Part 4: Field Extraction (CSV/TSV/Delimited)
# Print second field (comma-separated) ruby -F, -ane 'puts $F[1]' data.csv # Perl: perl -F, -ane 'print $F[1], "\n"' data.csv # Print first and third fields (tab-separated, default) ruby -ane 'puts "#{$F[0]}\t#{$F[2]}"' file.txt # Perl: perl -ane 'print "$F[0]\t$F[2]\n"' file.txt # Print last field ruby -ane 'puts $F[-1]' file.txt # Perl: perl -ane 'print $F[-1], "\n"' file.txt # Reorder fields ruby -F, -ane 'puts [$F[2], $F[0], $F[1]].join(",")' data.csv # Sum a numeric column (third field) ruby -ane 'BEGIN{$s=0}; $s += $F[2].to_f; END{puts $s}' file.txt # Perl: perl -ane '$s += $F[2]; END{print "$s\n"}' file.txt
Part 5: Search and Replace
# Global substitution (like sed) ruby -pe 'gsub(%r~old~, "new")' file.txt # Perl: perl -pe 's/old/new/g' file.txt # In-place edit with backup ruby -i.bak -pe 'gsub(%r~old~, "new")' file.txt # Perl: perl -i.bak -pe 's/old/new/g' file.txt # In-place edit without backup ruby -i -pe 'gsub(%r~old~, "new")' file.txt # Replace only on lines matching a pattern ruby -pe 'gsub(%r~foo~, "bar") if /specific_line/' file.txt # Perl: perl -pe 's/foo/bar/g if /specific_line/' file.txt # Delete lines matching pattern (in-place) ruby -i -ne 'print unless /DELETE_ME/' file.txt # Perl: perl -i -ne 'print unless /DELETE_ME/' file.txt
Part 6: Multiple Files and ARGF
# Process multiple files (ARGF handles this automatically) ruby -ne 'puts $_ if /ERROR/' file1.txt file2.txt file3.txt # Print filename with matching lines ruby -ne 'puts "#{ARGF.filename}:#{$.}: #{$_}" if /ERROR/' *.log # Reset line numbers per file ruby -ne 'puts "#{ARGF.filename}:#{ARGF.file.lineno}: #{$_}" if /pattern/' *.txt
Part 7: Combining with Unix Pipes
# Use as a filter in a pipeline cat /var/log/syslog | ruby -ne 'puts $_ if /error/i' # Extract IPs and sort by frequency ruby -ne 'puts $1 if /(\d+\.\d+\.\d+\.\d+)/' access.log | sort | uniq -c | sort -rn # JSON pretty-print from stdin curl -s api.example.com/data | ruby -rjson -e 'puts JSON.pretty_generate(JSON.parse(STDIN.read))' # Quick CSV to TSV ruby -F, -ane 'puts $F.join("\t")' data.csv > data.tsv
Part 8: Handy Switch Combos
Memorize these combos. They're the verbs of Ruby one-liner grammar.# -l -n -e : chomp input, loop lines, execute code # -l -a -n -e : chomp, auto-split, loop, execute # -l -a -F, -n -e : chomp, split on comma, loop, execute # -p -i.bak : loop with auto-print, in-place edit with backup
Created By: Wildcard Wizard. Copyright 2026