Skip to content

Commit

Permalink
Add ruby-one-liners
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshCheek committed Dec 16, 2011
1 parent 736db90 commit f3e1222
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
1 change: 1 addition & 0 deletions Readme.mdown
Expand Up @@ -25,6 +25,7 @@ Playgrounds
* [ready-for-zero-challenges](http://github.com/JoshCheek/Play/tree/master/ready-for-zero-challenges/) My solutions to [https://www.readyforzero.com/challenge](https://www.readyforzero.com/challenge).
* [ruby-overloadable](http://github.com/JoshCheek/Play/tree/master/ruby-overloadable/) Give Ruby method overloading
* [ruby-golf](http://github.com/JoshCheek/Play/tree/master/ruby-golf/) Solutions for [Ruby Golf](http://rubysource.com/ruby-golf/)
* [ruby-one-liners](http://github.com/JoshCheek/Play/tree/master/ruby-one-liners/) Using Ruby for command line one-liners (where you might normally use sed or awk).
* [ruby-s](http://github.com/JoshCheek/Play/tree/master/ruby-s/) How to use ruby's -s flag for simple command line args.
* [self-referential-hash](http://github.com/JoshCheek/Play/tree/master/self-referential-hash/) Hash where you can declare one key references another.
* [sikulidraw](http://github.com/JoshCheek/Play/tree/master/sikulidraw/) Generate commands for Sikuli such that it draws a picture on the screen, given that the mouse is a 1px pencil.
Expand Down
120 changes: 120 additions & 0 deletions ruby-one-liners/Readme.md
@@ -0,0 +1,120 @@
For Chicago Ruby's [Hack Night](http://www.meetup.com/ChicagoRuby/events/43150262/) on 15 Dec 2011:

Inspired by the [one-line scripts for awk](http://www.pement.org/awk/awk1line.txt),
we'll write Ruby programs to do things
like double-space a file or add line numbers to the file or center the text in each
line of a file. The challenge is to write the shortest possible program that works
for each of these tasks:

### doublespace a file

ruby -lpe '$_<<"\n"'


### add line numbers to each input file

# not happy with this one, but couldn't figure out how to get ARGF to reset $.
# (also, 1.9 only)
ruby -e 'l=-> line {puts "#$. #{line}"}; ARGV.each { |name| File.foreach name, &l}; $stdin.each &l'


### add line numbers for all files together

ruby -lne 'print "#$. #$_"'
ruby -lpe '$_.prepend "#$. "'


### add line numbers only for nonblank lines

ruby -lpe '/^$/ ? $.-=1 : $_ = "#$. :#$_"'


### count lines in file

ruby -ne 'END{puts $.}'


### count words in file

ruby -ane 'w = (w||0) + $F.size; END { p w }'


### output total number of lines that contain 'abc'

ruby -ne 'BEGIN { num = 0 }; num += 1 if $_ =~ /abc/; END { p num }'
ruby -e 'p ARGF.readlines.grep(/abc/).size'
ruby -ne 'w=(w||0)+1 if /abc/; END{p w}'


### output a string of 43 X's

ruby -e 'puts "X"*43'


### insert a string of 3 X's after column 6 of each input line

ruby -ple '$_.insert 6, "xxx" if $_.size > 6'


### delete leading whitespace from beginning of each line

ruby -pe '$_.sub! /^\s+/, ""'


### delete trailing whitespace from end of each line

ruby -pe '$_.gsub! /\s+$/, "\n"'


### add leading blanks to right-align all text in 80-column width

ruby -pe '$_ = "%80s\n" % $_.chomp'


### add leading and trailing blanks to center each line in 80 columns

ruby -lne 'printf "%80s\n", $_'


### reverse the text in each line

ruby -lpe '$_.reverse!'


### concatenate every 5 lines of input, using , as separator

ruby -e 'ARGF.each_slice(5) { |lines| puts lines.map(&:chomp).join(",") }'


### print first 10 lines of file

ruby -pe 'exit if $. > 10'


### print last 10 lines of file

ruby -ne 'puts $_ if $. > 10'


### print line 13 of file

ruby -ne 'puts $_ if $. == 13'


### delete blank lines from file

ruby -ne 'puts $_ unless $_ == "\n"'


### delete consecutive duplicate lines from file

ruby -ne 'puts $_ if /^[^\n]/../^$/'


### delete any duplicate lines from file

# consecutive duplicates
ruby -lne 'BEGIN{last=""};puts last=$_ unless last==$_'

# any duplicate in input
ruby -lne 'BEGIN{lines=Hash.new{|h,l|puts h[l]=l}};lines[$_]'

0 comments on commit f3e1222

Please sign in to comment.