This post originated from an RSS feed registered with Python Buzz
by Ryan Tomayko.
Original Post: HANDY ONE-LINERS FOR RUBY
Feed Title: Ryan Tomayko (weblog/python)
Feed URL: http://tomayko.com/feed/
Feed Description: Entries classified under Python.
Nice list of Ruby one-liners when working at the shell. e.g., emulating nl(1):
# number each line of a file (left justified).
$ ruby -ne 'printf("%-6s%s", $., $_)' < file.txt
# number each line of a file (right justified).
$ ruby -ne 'printf("%6s%s", $., $_)' < file.txt
# number each line of a file, only print non-blank lines
$ ruby -e 'while gets; end; puts $.' < file.txt
Unlike some other things, the -p and -e switches are something I've always been glad ruby adopted from perl.