Over at http://fepus.net/ruby1line.txt you can find an awesome collection of ruby 1-liners. Here are some samples:
# align all text flush right on a 79-column width
    $  ruby -ne 'printf("%79s", $_)' < file.txt

# center all text in middle of 79-column width
    $  ruby -ne 'puts $_.chomp.center(79)' < file.txt
    $  ruby -lne 'puts $_.center(79)' < file.txt

# substitute (find and replace) "foo" with "bar" on each line
    $  ruby -pe 'gsub(/foo/, "bar")' < file.txt

# substitute "foo" with "bar" ONLY for lines which contain "baz"
    $  ruby -pe 'gsub(/foo/, "bar") if $_ = /baz/' < file.txt

# substitute "foo" with "bar" EXCEPT for lines which contain "baz"

$ ruby -pe ‘gsub(/foo/, "bar";) unless $_ =~ /baz/' < file.txt

Comments